-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivation.nix
77 lines (67 loc) · 1.92 KB
/
activation.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
{ lib, pkgs }:
let
activation-bin-path = lib.makeBinPath (
with pkgs;
[
nix
bash
coreutils
findutils
]
);
cleanup = pkgs.writeShellScript "cleanup" ''
old_dotfiles=$1
new_dotfiles=$2
shift
shift
for src_path in "$@"; do
rel_path="''${src_path#$old_dotfiles/}"
dst_path="$HOME/$rel_path"
[[ -e "$new_dotfiles/$rel_path" ]] && continue
[[ ! -L "$dst_path" ]] && continue
rm "$dst_path"
done
'';
link = pkgs.writeShellScript "link" ''
new_dotfiles=$1
shift
for src_path in "$@"; do
rel_path="''${src_path#$new_dotfiles/}"
dst_path="$HOME/$rel_path"
[[ -L "$dst_path" ]] && collision_opt='f' || collision_opt='b'
mkdir -p "$(dirname "$dst_path")"
ln -s$collision_opt "$src_path" "$dst_path"
done
'';
in
pkgs.writeShellScript "activation-script" ''
export PATH="${activation-bin-path}"
log_info() { echo "homini: $@"; }
log_error() { >&2 echo "homini: $@"; }
xdg_state_home="''${XDG_STATE_HOME:-$HOME/.local/state}"
gcroots="$xdg_state_home/homini/gcroots/homini"
if [[ -e $gcroots ]]; then
old_path="$(readlink -e "$gcroots")"
old_dotfiles=$(readlink -e "$old_path/homini-dotfiles")
fi
new_path="@OUT@"
new_dotfiles=$(readlink -e "$new_path/homini-dotfiles")
if [[ "$old_dotfiles" == "$new_dotfiles" ]]; then
log_info "no dotfiles changes"; exit 0
fi
cleanup_old_dotfiles() {
[[ ! -e "$old_dotfiles" ]] && return
find $old_dotfiles -type f -exec bash ${cleanup} $old_dotfiles $new_dotfiles {} +
}
switch_gcroots() {
[[ "$old_path" == "$new_path" ]] && return
nix-store --realise "$new_path" --add-root "$gcroots" > /dev/null
}
link_new_dotfiles() {
local new_dotfiles=$(readlink -e $new_path/homini-dotfiles)
find $new_dotfiles -type f -exec bash ${link} $new_dotfiles {} +
}
cleanup_old_dotfiles
switch_gcroots
link_new_dotfiles
''