Skip to content

Commit 4677f6c

Browse files
authored
Merge pull request #625 from nix-community/image-fixes
Fix building images when non-binary reproducible builds are present
2 parents 874c83c + 718565d commit 4677f6c

File tree

6 files changed

+40
-39
lines changed

6 files changed

+40
-39
lines changed

disko-install

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,13 @@ main() {
207207
--arg diskMappings "$(serialiaseArrayToNix diskMappings)" \
208208
--argstr extraSystemConfig "$extraSystemConfig" \
209209
-A installToplevel \
210+
-A closureInfo \
210211
-A "$diskoAttr")
211212

212213
IFS=$'\n' mapfile -t artifacts <<<"$outputs"
213214
nixos_system=${artifacts[0]}
214-
disko_script=${artifacts[1]}
215+
closure_info=${artifacts[1]}
216+
disko_script=${artifacts[2]}
215217

216218
if [[ -n ${dry_run-} ]]; then
217219
echo "Would run: $disko_script"
@@ -227,6 +229,19 @@ main() {
227229
cp -ar "$source" "$mountPoint/$destination"
228230
done
229231

232+
# nix copy uses up a lot of memory and we work around issues with incorrect checksums in our store
233+
# that can be caused by using closureInfo in combination with multiple builders and non-deterministic builds.
234+
# Therefore if we have a blank store, we copy the store paths and registration from the closureInfo.
235+
if [[ ! -d "${mountPoint}/nix/store" ]]; then
236+
export NIX_STATE_DIR=${mountPoint}/nix/var/nix
237+
echo "Copying store paths" >&2
238+
mkdir -p "${mountPoint}/nix/store"
239+
xargs cp --recursive --target "${mountPoint}/nix/store" < "${closure_info}/store-paths"
240+
echo "Loading nix database" >&2
241+
nix-store --load-db < "${closure_info}/registration"
242+
unset NIX_STATE_DIR
243+
fi
244+
230245
nixos-install --no-root-password --system "$nixos_system" --root "$mountPoint"
231246
}
232247

disko-install.nix

Lines changed: 0 additions & 26 deletions
This file was deleted.

docs/disko-install.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,10 @@ Add this to your flake.nix output:
197197
{ pkgs, self, ... }:
198198
let
199199
dependencies = [
200-
pkgs.stdenv.drvPath
201200
self.nixosConfigurations.your-machine.config.system.build.toplevel
202201
self.nixosConfigurations.your-machine.config.system.build.diskoScript
202+
self.nixosConfigurations.your-machine.pkgs.stdenv.drvPath
203+
(self.nixosConfigurations.your-machine.pkgs.closureInfo { rootPaths = [ ]; }).drvPath
203204
] ++ builtins.map (i: i.outPath) (builtins.attrValues self.inputs);
204205
205206
closureInfo = pkgs.closureInfo { rootPaths = dependencies; };

install-cli.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,8 @@ let
6262
in
6363
{
6464
installToplevel = installSystem.config.system.build.toplevel;
65+
closureInfo = installSystem.pkgs.closureInfo {
66+
rootPaths = [ installSystem.config.system.build.toplevel ];
67+
};
6568
inherit (diskoSystem.config.system.build) formatScript mountScript diskoScript;
6669
}

lib/make-disk-image.nix

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ let
3838
${lib.concatMapStringsSep "\n" (disk: "mv ${disk.name}.raw \"$out\"/${disk.name}.raw") (lib.attrValues nixosConfig.config.disko.devices.disk)}
3939
${extraPostVM}
4040
'';
41+
42+
closureInfo = pkgs.closureInfo {
43+
rootPaths = [ systemToInstall.config.system.build.toplevel ];
44+
};
4145
partitioner = ''
4246
# running udev, stolen from stage-1.sh
4347
echo "running udev..."
@@ -53,16 +57,19 @@ let
5357
udevadm trigger --action=add
5458
udevadm settle
5559
56-
# populate nix db, so nixos-install doesn't complain
57-
export NIX_STATE_DIR=$TMPDIR/state
58-
nix-store --load-db < ${pkgs.closureInfo {
59-
rootPaths = [ systemToInstall.config.system.build.toplevel ];
60-
}}/registration
61-
6260
${systemToInstall.config.system.build.diskoScript}
6361
'';
62+
6463
installer = ''
65-
${systemToInstall.config.system.build.nixos-install}/bin/nixos-install --system ${systemToInstall.config.system.build.toplevel} --keep-going --no-channel-copy -v --no-root-password --option binary-caches ""
64+
# populate nix db, so nixos-install doesn't complain
65+
export NIX_STATE_DIR=${systemToInstall.config.disko.rootMountPoint}/nix/var/nix
66+
nix-store --load-db < "${closureInfo}/registration"
67+
68+
# We copy files with cp because `nix copy` seems to have a large memory leak
69+
mkdir -p ${systemToInstall.config.disko.rootMountPoint}/nix/store
70+
xargs cp --recursive --target ${systemToInstall.config.disko.rootMountPoint}/nix/store < ${closureInfo}/store-paths
71+
72+
${systemToInstall.config.system.build.nixos-install}/bin/nixos-install --root ${systemToInstall.config.disko.rootMountPoint} --system ${systemToInstall.config.system.build.toplevel} --keep-going --no-channel-copy -v --no-root-password --option binary-caches ""
6673
umount -Rv ${systemToInstall.config.disko.rootMountPoint}
6774
'';
6875
QEMU_OPTS = lib.concatMapStringsSep " " (disk: "-drive file=${disk.name}.raw,if=virtio,cache=unsafe,werror=report,format=raw") (lib.attrValues nixosConfig.config.disko.devices.disk);

tests/disko-install/default.nix

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{ pkgs ? import <nixpkgs> { }, self }:
22
let
3-
disko-install = pkgs.callPackage ../../disko-install.nix { };
3+
disko = pkgs.callPackage ../../package.nix { };
44

55
dependencies = [
6-
pkgs.stdenv.drvPath
6+
self.nixosConfigurations.testmachine.pkgs.stdenv.drvPath
7+
(self.nixosConfigurations.testmachine.pkgs.closureInfo { rootPaths = []; }).drvPath
78
self.nixosConfigurations.testmachine.config.system.build.toplevel
89
self.nixosConfigurations.testmachine.config.system.build.diskoScript
910
] ++ builtins.map (i: i.outPath) (builtins.attrValues self.inputs);
@@ -36,9 +37,9 @@ pkgs.nixosTest {
3637
permission = machine.succeed("stat -c %a /tmp/age.key").strip()
3738
assert permission == "600", f"expected permission 600 on /tmp/age.key, got {permission}"
3839
39-
machine.succeed("${disko-install}/bin/disko-install --disk main /dev/vdb --extra-files /tmp/age.key /var/lib/secrets/age.key --flake ${../..}#testmachine")
40+
machine.succeed("${disko}/bin/disko-install --disk main /dev/vdb --extra-files /tmp/age.key /var/lib/secrets/age.key --flake ${../..}#testmachine")
4041
# test idempotency
41-
machine.succeed("${disko-install}/bin/disko-install --mode mount --disk main /dev/vdb --flake ${../..}#testmachine")
42+
machine.succeed("${disko}/bin/disko-install --mode mount --disk main /dev/vdb --flake ${../..}#testmachine")
4243
machine.shutdown()
4344
4445
new_machine = create_test_machine(oldmachine=machine, args={ "name": "after_install" })

0 commit comments

Comments
 (0)