-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault.nix
225 lines (201 loc) · 7.35 KB
/
default.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
## Build an instance of a ALX system from the ALX-branded nixpkgs tree
## in the ./nixpkgs submodule. This includes an install image and
## configuration that can be used to install a system from scratch
## with the installer provided by installer.nix and a shell script
## that performs an upgrade of an existing ALX system to the new
## version.
{ system ? "x86_64-linux" }:
with import ./nixpkgs { inherit system; };
with lib;
let
submoduleWithDotGitToStore = parent: modulePath: moduleName:
runCommand "submodule-${moduleName}-with-dotgit"
rec { parentPath = builtins.toPath parent;
parentFiltered = builtins.filterSource
(path: type:
path == parentPath + "/.git" || path == parentPath + "/.git/modules" ||
hasPrefix (parentPath + "/.git/modules/" + moduleName) path ||
hasPrefix (parentPath + "/" + modulePath) path) parent; }
''
set -e
cd $parentFiltered"/"${modulePath}
mkdir $out
tar cpf - . | (cd $out && tar xpf -)
if [ -d .git ]; then
exit 0
elif [ ! -f .git ]; then
echo "missing .git"
exit 1
fi
gitdir=$(cat .git | cut -d' ' -f2)
if [[ $gitdir =~ ^/ ]]; then
## gitdir is supposed to be a relative path, but it isn't always,
## depending on how the submodule has been checked out and on the
## version of git.
gitdir=$(realpath --canonicalize-missing --relative-to $parentPath"/"${modulePath} $gitdir)
fi
chmod u+w $out
rm -f $out/.git
cp -prd $gitdir $out/.git
chmod u+w $out/.git
grep -v worktree $out/.git/config >$out/.git/config.new
mv $out/.git/config.new $out/.git/config
'';
installImageConfig = {
installImage = {
## Derive the client's configuration from the "branded" nixpkgs
## system in the nixpkgs submodule.
nixpkgs = {
path = submoduleWithDotGitToStore ./. "nixpkgs" "nixpkgs";
stableBranch = true;
};
inherit system;
## The contents of this directory will be copied as is to
## /etc/nixos on the install target.
nixosConfigDir = ./nixos-config;
## Tacacs support is disabled by default. Declare the tacplus
## packages here to make it part of the Nix store on the install
## image
additionalPkgs = with import ./nixpkgs {};
[ exabgp
pam_tacplus
nss_tacplus
];
};
};
customConfig = ./install-image-config.nix;
build = (import ./nixpkgs/nixos/lib/eval-config.nix {
inherit system;
modules = [ installer/modules/install-image.nix
installImageConfig
] ++ (optional (pathExists customConfig) customConfig);
}).config.system.build;
channel = build.installImage.channel;
releaseName = builtins.unsafeDiscardStringContext
(builtins.substring 33 (-1) (baseNameOf channel));
version = getVersion releaseName;
versionALX = writeText "ALX-version"
''
${version}
'';
manpages = build.manual.manpages;
manpageASCII =
pkgs.runCommand "manpage-ascii"
{}
''
mkdir $out
${pkgs.man}/bin/man ${manpages}/share/man/man5/configuration.nix.5 \
| ${pkgs.utillinux}/bin/col -bx >$out/configuration.nix.5
'';
upgradeCommand = let
upgradeScript = writeScript "upgrade"
''
#!/run/current-system/sw/bin/bash
set -e
info() {
echo "This archve contains ALX version ${version}"
echo "The current system is running version $current"
exit 0;
}
current=$(nixos-version | awk '{print $1}')
while getopts if opt; do
case $opt in
i) info;;
f) force=1;;
esac
done
echo "Attempting to upgrade the system from version $current to ${version}"
set +e
nix-instantiate --eval -E '<nixpkgs>' >/dev/null
have_nixpkgs=$?
set -e
if [ $have_nixpkgs -eq 0 ]; then
if [ $(nix-instantiate --eval -E "with (import <nixpkgs> {}).lib; versionOlder \"$current\" \"${version}\"") != "true" \
-a -z "$force" ]; then
echo "Target version is not newer than current version, use -f to " \
"force installation"
exit 1
fi
else
if [ -z "$force" ]; then
echo "nixpkgs not available, can't compare versions. Use -f to force installation"
exit 1
fi
fi
set -- $(nix-channel --list | awk '$1 == "nixos" {print $2}')
url=$1
if [ ! -n "$url" ]; then
echo "Channel \"nixos\" required for upgrade but is not configured (check \"nix-channel --list\")"
exit 1
fi
set -- $(echo $url | cut -d: --output-delimiter " " -f1,2)
method=$1
loc=$2
if [ "$method" != "file" ]; then
echo "Upgrades are only supported for method \"file:\""
exit 1;
fi
loc=$(echo $loc | sed -e 's!^//!!')
dir=$(dirname $loc)
if [ ! -d $dir ]; then
echo "Directory $dir doesn't exist, creating"
mkdir -p $dir
fi
if [ -e $loc -a ! -L $loc ]; then
echo "$loc is expected to be a symbolic link: $(type $loc)"
exit 1;
fi
if [ -d $dir/${releaseName} ]; then
echo "$dir/${releaseName} already exists, remove manually to force upgrade"
exit 1;
fi
cat ${releaseName}.tar | (cd $dir && tar xpf -)
rm -f $loc
ln -s ./${releaseName} $loc
echo "Updating nixos channel"
nix-channel --update
echo "Reconfiguring system"
nixos-rebuild switch
echo "Upgrade completed, use \"nix-env -p /nix/var/nix/profiles/per-user/root/channels --rollback\" " \
"to revert"
'';
selfExtractor = writeScript "self-extractor"
''
#!/run/current-system/sw/bin/bash
export TMPDIR=$(mktemp -d /tmp/selfextract.XXXXXX)
archive=$(awk '/^___ARCHIVE_BELOW___/ {print NR + 1; exit 0; }' $0)
tail -n+$archive $0 | tar x -C $TMPDIR
cwd=$(pwd)
cd $TMPDIR
./upgrade "$@"
cd $cwd
rm -rf $TMPDIR
exit 0
___ARCHIVE_BELOW___
'';
in runCommand "${releaseName}"
{}
''
path=$out/${releaseName}
mkdir -p $path
(cd ${channel} && tar --transform="s/^nixos/${releaseName}/" -cJf $path/nixexprs.tar.xz nixos)
cat ${channel}/binary-caches/nixos >$path/binary-cache-url
(cd $out && tar cf ${releaseName}.tar ${releaseName})
rm -rf $path
cp -p ${upgradeScript} $out/upgrade
(cd $out && tar cf payload.tar ${releaseName}.tar upgrade)
cat ${selfExtractor} $out/payload.tar >$out/alx-upgrade
chmod --reference=${selfExtractor} $out/alx-upgrade
rm $out/${releaseName}.tar $out/upgrade $out/payload.tar
release_notes=${copyPathToStore ./release-notes}/${version}
[ -f $release_notes ] || { echo "Missing release notes $release_notes"; exit 1; }
cp $release_notes $out/release-notes.txt
'';
jobs = rec {
inherit manpages manpageASCII;
installImage = build.installImage.tarball;
installConfig = build.installImage.config;
inherit upgradeCommand versionALX;
};
in
jobs