This repository has been archived by the owner on Sep 20, 2024. It is now read-only.
forked from sheenobu/nix-home
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nix-home.sh
executable file
·187 lines (160 loc) · 4.46 KB
/
nix-home.sh
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env bash
set -e
warn() { echo WARNING: "$@" >&2; }
info() { echo "$@" >&2; }
here=$(dirname "${BASH_SOURCE[0]}")
NIXHOME=${NIXHOME:-$here/lib}
performLink="true"
op=""
if [[ $# -eq 0 ]]; then
op="--set"
fi
for i in "$@"; do
case $i in
--version)
echo "0.3.1";
exit
;;
--dry-run)
performLink="false"
;;
# we only really need to list options which disable linking. Everything else
# is passed directly to nix-env
--list-generations)
performLink="false"
;;
--delete-generations)
performLink="false"
;;
--help)
nix-env --help
exit
;;
esac
done
if [[ ! -e "$HOME/default.nix" ]]; then
warn "No $HOME/default.nix file found, refusing to run"
exit 1
fi
# TODO: we may want to complain if default.nix is a symlink. It can be a
# symlink but has some relative path issues.
# The trailing / is important for find to work properly
OLDOLDROOT=/var/run/user/$(id -u)/current-home/
OLDROOT=$HOME/.nix-home/
ROOT=/nix/var/nix/profiles/per-user/$USER/nix-home/
prefix=$ROOT
TMPFILE=$(mktemp -u --suffix=nixhome)
fail() {
warn "Failed to run"
rm -f "$TMPFILE"
exit 1
}
if [[ "$performLink" = "true" ]]; then
# enumerate over every file in current-home, store in TMPFILE
touch "$TMPFILE"
# 0.1.x
if [[ -e "$OLDOLDROOT" ]]; then
find "$OLDOLDROOT" -type f >> "$TMPFILE"
find "$OLDOLDROOT" -type l >> "$TMPFILE"
fi
# 0.2.x
if [[ -e "$OLDROOT" ]]; then
find "$OLDROOT" -type f >> "$TMPFILE"
find "$OLDROOT" -type l >> "$TMPFILE"
fi
# 0.3.x and beyond
if [[ -e "$ROOT" ]]; then
find "$ROOT" -type f >> "$TMPFILE"
find "$ROOT" -type l >> "$TMPFILE"
fi
fi
nix_env_cmd=(
nix-env -I "$NIXHOME" -p "$ROOT" -f "$HOME" "$op" "$@"
)
if [[ "$PRINTCMDS" = "true" ]]; then
info "${nix_env_cmd[@]}"
fi
# run the actual command
"${nix_env_cmd[@]}" || fail
# quit if we have skipped linking
if [[ "$performLink" != "true" ]]; then
exit
fi
# link all symlinks in the overlay to $HOME
while IFS= read -r -d '' x; do
dest=${x#$prefix}
if [[ ! -h "$HOME/$dest" ]]; then
if [[ -f "$HOME/$dest" ]]; then
warn "$dest is a regular file; refusing to overwrite with link"
elif [[ -d "$HOME/$dest" ]]; then
warn "$dest is a regular directory; refusing to overwrite with link"
else
# dest does not exist
info "linking $x to $dest"
mkdir -p "$(dirname "$HOME/$dest")"
ln -sf "$x" -t "$(dirname "$HOME/$dest")"
fi
else
# dest file or dir is a symbolic link
originalFile=$(readlink "$HOME/$dest") # ensure the existing link goes to a nix-home file, otherwise ignore it.
case $originalFile in
$ROOT* | $OLDROOT* | $OLDOLDROOT*)
if [[ "$originalFile" != "$x" ]]; then
info "linking $x to $dest"
mkdir -p "$(dirname "$HOME/$dest")"
ln -sf "$x" -t "$(dirname "$HOME/$dest")"
fi
;;
*)
warn "$dest is a symlink to a non nix-home file; refusing to overwrite with link"
;;
esac
fi
done < <(find "$ROOT" -type l -print0)
# link all the regular files in the overlay to $HOME
while IFS= read -r -d '' x; do
dest=${x#$prefix}
if [[ ! -h "$HOME/$dest" ]]; then
if [[ -f "$HOME/$dest" ]]; then
warn "$dest is a regular file; refusing to overwrite with link"
elif [[ -d "$HOME/$dest" ]]; then
warn "$dest is a regular directory; refusing to overwrite with link"
else
# dest does not exist
info "linking $x to $dest"
mkdir -p "$(dirname "$HOME/$dest")"
ln -sf "$x" -t "$(dirname "$HOME/$dest")"
fi
else
# dest file or dir is a symbolic link
originalFile=$(readlink "$HOME/$dest")
case $originalFile in
$ROOT* | $OLDROOT* | $OLDOLDROOT*)
if [[ "$originalFile" != "$x" ]]; then
info "linking $x to $dest"
mkdir -p "$(dirname "$HOME/$dest")"
ln -sf "$x" -t "$(dirname "$HOME/$dest")"
fi
;;
*)
warn "$dest is a symlink to a non nix-home file; refusing to overwrite with link"
;;
esac
fi
done < <(find "$ROOT" -type f -print0)
# iterate over all files in TMPFILE
while IFS= read -r -d '' x; do
if [[ ! -e "$x" ]]; then
info "$x does not exist in new home"
dest=${x#$prefix}
if [[ -h "$HOME/$dest" ]]; then
info "unlinking $HOME/$dest"
rm "$HOME/$dest"
else
warn "$HOME/$dest is not a symbolic link, skipping"
fi
fi
done < "$TMPFILE"
# cleanup
rm "$TMPFILE"
info "Done!"