From e5b3299a14b2af1ae4fa52f7ca2f38cd3d0374b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Mon, 29 Apr 2024 17:41:01 +0200 Subject: [PATCH 1/6] README: link to disko-install and disko-images --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d5de931..2eb7e2b 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,9 @@ recursive layouts. Disko doesn't require installation: it can be run directly from nix-community repository. The [Quickstart Guide](./docs/quickstart.md) documents how to run -Disko in its simplest form when installing NixOS. +Disko in its simplest form when installing NixOS. Alternativly you can +also use the new [disko-install](./docs/disko-install.md) tool, which combines +the `disko` and `nixos-install` into one step. For information on other use cases, including upgrading from an older version of **disko**, using **disko** without NixOS and downloading the module, see the @@ -53,6 +55,8 @@ For more detailed options, such as command line switches, see the To access sample configurations for commonly-used disk layouts, refer to the [examples](./example) provided. +Disko can be also used to create [disk images](./docs/disko-images.md). + ## Sample Configuration and CLI command A simple disko configuration may look like this: From 4f8c8580b45c0ce69bf22a0b2bc5743db50d9c12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Mon, 6 May 2024 19:29:20 +0200 Subject: [PATCH 2/6] fix building images when non-binary reproducible builds are present closure-info has one flaw, it's possible that this file contains stale information when build are not reproducible and the local checksum doesn't match what the remote build created. The work-around here is that we disregard the hashes and re-compute with what nar's we actually have locally instead. --- lib/closure-info.nix | 45 +++++++++++++++++++++++++++++++++++++++++ lib/make-disk-image.nix | 4 +++- 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 lib/closure-info.nix diff --git a/lib/closure-info.nix b/lib/closure-info.nix new file mode 100644 index 0000000..e40fe18 --- /dev/null +++ b/lib/closure-info.nix @@ -0,0 +1,45 @@ +# This is a modified version of the closure-info derivation from nixpkgs. +# Unlike the original, it does not include hashes in the registration file, +# which might be incorrect if a build is not binary reproducible. + +# This derivation builds two files containing information about the +# closure of 'rootPaths': $out/store-paths contains the paths in the +# closure, and $out/registration contains a file suitable for use with +# "nix-store --register-validity". + +{ stdenv, coreutils, jq }: + +{ rootPaths }: + +assert builtins.langVersion >= 5; + +stdenv.mkDerivation { + name = "closure-info"; + + __structuredAttrs = true; + + exportReferencesGraph.closure = rootPaths; + + preferLocalBuild = true; + + nativeBuildInputs = [ coreutils jq ]; + + empty = rootPaths == []; + + buildCommand = + '' + out=''${outputs[out]} + + mkdir $out + + if [[ -n "$empty" ]]; then + echo 0 > $out/total-nar-size + touch $out/registration $out/store-paths + else + jq -r ".closure | map(.narSize) | add" < "$NIX_ATTRS_JSON_FILE" > $out/total-nar-size + jq -r '.closure | map([.path, "", (.references | length)] + .references) | add | map("\(.)\n") | add' < "$NIX_ATTRS_JSON_FILE" | head -n -1 > $out/registration + jq -r '.closure[].path' < "$NIX_ATTRS_JSON_FILE" > $out/store-paths + fi + + ''; +} diff --git a/lib/make-disk-image.nix b/lib/make-disk-image.nix index 7bbc021..69db53e 100644 --- a/lib/make-disk-image.nix +++ b/lib/make-disk-image.nix @@ -38,6 +38,8 @@ let ${lib.concatMapStringsSep "\n" (disk: "mv ${disk.name}.raw \"$out\"/${disk.name}.raw") (lib.attrValues nixosConfig.config.disko.devices.disk)} ${extraPostVM} ''; + + closureInfo = pkgs.callPackage ./closure-info.nix { }; partitioner = '' # running udev, stolen from stage-1.sh echo "running udev..." @@ -55,7 +57,7 @@ let # populate nix db, so nixos-install doesn't complain export NIX_STATE_DIR=$TMPDIR/state - nix-store --load-db < ${pkgs.closureInfo { + ${pkgs.fakeroot}/bin/fakeroot nix-store --register-validity --reregister < ${closureInfo { rootPaths = [ systemToInstall.config.system.build.toplevel ]; }}/registration From 8a4585a200aeec1afdc32bd0d495e26b66d828ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Mon, 6 May 2024 20:20:01 +0200 Subject: [PATCH 3/6] disko-images: replace nix-copy with cp This hopefully fixes the large memory consumption we have seen in the past. Also presumably coreutils cp is more tuned for performance than whatever nix does. --- lib/make-disk-image.nix | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/make-disk-image.nix b/lib/make-disk-image.nix index 69db53e..87120d5 100644 --- a/lib/make-disk-image.nix +++ b/lib/make-disk-image.nix @@ -39,7 +39,9 @@ let ${extraPostVM} ''; - closureInfo = pkgs.callPackage ./closure-info.nix { }; + closureInfo = (pkgs.callPackage ./closure-info.nix { }) { + rootPaths = [ systemToInstall.config.system.build.toplevel ]; + }; partitioner = '' # running udev, stolen from stage-1.sh echo "running udev..." @@ -55,16 +57,23 @@ let udevadm trigger --action=add udevadm settle - # populate nix db, so nixos-install doesn't complain - export NIX_STATE_DIR=$TMPDIR/state - ${pkgs.fakeroot}/bin/fakeroot nix-store --register-validity --reregister < ${closureInfo { - rootPaths = [ systemToInstall.config.system.build.toplevel ]; - }}/registration - ${systemToInstall.config.system.build.diskoScript} ''; + installer = '' - ${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 "" + # populate nix db, so nixos-install doesn't complain + export NIX_STATE_DIR=${systemToInstall.config.disko.rootMountPoint}/nix/var/nix + # We have to use fakeroot here, because nix tries to chown files in the original store. + # fakeroot will make override all chown calls to no-ops. + echo "Registering store paths..." + ${pkgs.fakeroot}/bin/fakeroot nix-store --register-validity --reregister < ${closureInfo}/registration + + # We copy files with cp because `nix copy` seems to have a large memory leak + echo "Copying store paths..." + mkdir -p ${systemToInstall.config.disko.rootMountPoint}/nix/store + xargs cp -r --target-directory=${systemToInstall.config.disko.rootMountPoint}/nix/store < ${closureInfo}/store-paths + + ${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 "" umount -Rv ${systemToInstall.config.disko.rootMountPoint} ''; 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); From cdefe26742f442351e73ce0f7caa3f559be32dc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Tue, 7 May 2024 07:40:29 +0200 Subject: [PATCH 4/6] load database with checksum again but do the same in disko-install --- disko-install | 21 ++++++++++++++- disko-install.nix | 22 ++++++++++++---- docs/disko-install.md | 3 ++- install-cli.nix | 3 +++ lib/closure-info.nix | 45 --------------------------------- lib/make-disk-image.nix | 11 +++----- tests/disko-install/default.nix | 3 ++- 7 files changed, 48 insertions(+), 60 deletions(-) delete mode 100644 lib/closure-info.nix diff --git a/disko-install b/disko-install index db55196..19a13bc 100755 --- a/disko-install +++ b/disko-install @@ -207,11 +207,13 @@ main() { --arg diskMappings "$(serialiaseArrayToNix diskMappings)" \ --argstr extraSystemConfig "$extraSystemConfig" \ -A installToplevel \ + -A closureInfo \ -A "$diskoAttr") IFS=$'\n' mapfile -t artifacts <<<"$outputs" nixos_system=${artifacts[0]} - disko_script=${artifacts[1]} + closure_info=${artifacts[1]} + disko_script=${artifacts[2]} if [[ -n ${dry_run-} ]]; then echo "Would run: $disko_script" @@ -227,6 +229,23 @@ main() { cp -ar "$source" "$mountPoint/$destination" done + # nix copy uses up a lot of memory and we work around issues with incorrect checksums in our store + # that can be caused by using closureInfo in combination with multiple builders and non-deterministic builds. + # Therefore if we have a blank store, we copy the store paths and registration from the closureInfo. + if [[ ! -d "${mountPoint}/nix/store" ]]; then + export NIX_STATE_DIR=${mountPoint}/nix/var/nix + echo "Copying store paths" >&2 + + if [ -t 1 ]; then + xargs -I% xcp --recursive % "${mountPoint}/nix/store" < "${closure_info}/store-paths" + else + xargs -I% xcp --recursive --no-progress % "${mountPoint}/nix/store" < "${closure_info}/store-paths" + fi + echo "Loading nix database" >&2 + nix-store --load-db < "${closure_info}/registration" + unset NIX_STATE_DIR + fi + nixos-install --no-root-password --system "$nixos_system" --root "$mountPoint" } diff --git a/disko-install.nix b/disko-install.nix index 48ff6ac..dbf124e 100644 --- a/disko-install.nix +++ b/disko-install.nix @@ -1,11 +1,16 @@ -{ stdenvNoCC, makeWrapper, lib }: +{ + stdenvNoCC, + makeWrapper, + lib, + coreutils, + xcp, + nixos-install-tools, +}: stdenvNoCC.mkDerivation { name = "disko-install"; src = ./.; - nativeBuildInputs = [ - makeWrapper - ]; + nativeBuildInputs = [ makeWrapper ]; installPhase = '' mkdir -p $out/bin $out/share/disko cp -r install-cli.nix $out/share/disko @@ -14,7 +19,14 @@ stdenvNoCC.mkDerivation { -e "s|#!/usr/bin/env.*|#!/usr/bin/env bash|" \ disko-install > $out/bin/disko-install chmod 755 $out/bin/disko-install - wrapProgram $out/bin/disko-install + wrapProgram $out/bin/disko-install \ + --prefix PATH : "${ + lib.makeBinPath [ + coreutils + xcp + nixos-install-tools + ] + }" ''; meta = with lib; { description = "Disko and nixos-install in one command"; diff --git a/docs/disko-install.md b/docs/disko-install.md index bec4f98..4476bb9 100644 --- a/docs/disko-install.md +++ b/docs/disko-install.md @@ -197,9 +197,10 @@ Add this to your flake.nix output: { pkgs, self, ... }: let dependencies = [ - pkgs.stdenv.drvPath self.nixosConfigurations.your-machine.config.system.build.toplevel self.nixosConfigurations.your-machine.config.system.build.diskoScript + self.nixosConfigurations.your-machine.pkgs.stdenv.drvPath + (self.nixosConfigurations.your-machine.pkgs.closureInfo { rootPaths = [ ]; }).drvPath ] ++ builtins.map (i: i.outPath) (builtins.attrValues self.inputs); closureInfo = pkgs.closureInfo { rootPaths = dependencies; }; diff --git a/install-cli.nix b/install-cli.nix index d1cd148..c173bf8 100644 --- a/install-cli.nix +++ b/install-cli.nix @@ -62,5 +62,8 @@ let in { installToplevel = installSystem.config.system.build.toplevel; + closureInfo = installSystem.pkgs.closureInfo { + rootPaths = [ installSystem.config.system.build.toplevel ]; + }; inherit (diskoSystem.config.system.build) formatScript mountScript diskoScript; } diff --git a/lib/closure-info.nix b/lib/closure-info.nix deleted file mode 100644 index e40fe18..0000000 --- a/lib/closure-info.nix +++ /dev/null @@ -1,45 +0,0 @@ -# This is a modified version of the closure-info derivation from nixpkgs. -# Unlike the original, it does not include hashes in the registration file, -# which might be incorrect if a build is not binary reproducible. - -# This derivation builds two files containing information about the -# closure of 'rootPaths': $out/store-paths contains the paths in the -# closure, and $out/registration contains a file suitable for use with -# "nix-store --register-validity". - -{ stdenv, coreutils, jq }: - -{ rootPaths }: - -assert builtins.langVersion >= 5; - -stdenv.mkDerivation { - name = "closure-info"; - - __structuredAttrs = true; - - exportReferencesGraph.closure = rootPaths; - - preferLocalBuild = true; - - nativeBuildInputs = [ coreutils jq ]; - - empty = rootPaths == []; - - buildCommand = - '' - out=''${outputs[out]} - - mkdir $out - - if [[ -n "$empty" ]]; then - echo 0 > $out/total-nar-size - touch $out/registration $out/store-paths - else - jq -r ".closure | map(.narSize) | add" < "$NIX_ATTRS_JSON_FILE" > $out/total-nar-size - jq -r '.closure | map([.path, "", (.references | length)] + .references) | add | map("\(.)\n") | add' < "$NIX_ATTRS_JSON_FILE" | head -n -1 > $out/registration - jq -r '.closure[].path' < "$NIX_ATTRS_JSON_FILE" > $out/store-paths - fi - - ''; -} diff --git a/lib/make-disk-image.nix b/lib/make-disk-image.nix index 87120d5..c9b42f9 100644 --- a/lib/make-disk-image.nix +++ b/lib/make-disk-image.nix @@ -28,6 +28,7 @@ let systemdMinimal nix util-linux + xcp ] ++ nixosConfig.config.disko.extraDependencies; preVM = '' ${lib.concatMapStringsSep "\n" (disk: "truncate -s ${disk.imageSize} ${disk.name}.raw") (lib.attrValues nixosConfig.config.disko.devices.disk)} @@ -39,7 +40,7 @@ let ${extraPostVM} ''; - closureInfo = (pkgs.callPackage ./closure-info.nix { }) { + closureInfo = pkgs.closureInfo { rootPaths = [ systemToInstall.config.system.build.toplevel ]; }; partitioner = '' @@ -63,15 +64,11 @@ let installer = '' # populate nix db, so nixos-install doesn't complain export NIX_STATE_DIR=${systemToInstall.config.disko.rootMountPoint}/nix/var/nix - # We have to use fakeroot here, because nix tries to chown files in the original store. - # fakeroot will make override all chown calls to no-ops. - echo "Registering store paths..." - ${pkgs.fakeroot}/bin/fakeroot nix-store --register-validity --reregister < ${closureInfo}/registration + nix-store --load-db < "${closureInfo}/registration" # We copy files with cp because `nix copy` seems to have a large memory leak - echo "Copying store paths..." mkdir -p ${systemToInstall.config.disko.rootMountPoint}/nix/store - xargs cp -r --target-directory=${systemToInstall.config.disko.rootMountPoint}/nix/store < ${closureInfo}/store-paths + xargs -I % xcp --recursive % ${systemToInstall.config.disko.rootMountPoint}/nix/store < ${closureInfo}/store-paths ${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 "" umount -Rv ${systemToInstall.config.disko.rootMountPoint} diff --git a/tests/disko-install/default.nix b/tests/disko-install/default.nix index 6360de4..ac85b66 100644 --- a/tests/disko-install/default.nix +++ b/tests/disko-install/default.nix @@ -3,7 +3,8 @@ let disko-install = pkgs.callPackage ../../disko-install.nix { }; dependencies = [ - pkgs.stdenv.drvPath + self.nixosConfigurations.testmachine.pkgs.stdenv.drvPath + (self.nixosConfigurations.testmachine.pkgs.closureInfo { rootPaths = []; }).drvPath self.nixosConfigurations.testmachine.config.system.build.toplevel self.nixosConfigurations.testmachine.config.system.build.diskoScript ] ++ builtins.map (i: i.outPath) (builtins.attrValues self.inputs); From babc1554e8a1f16ff7dfe2ea362cd84cd8e3ba73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Tue, 7 May 2024 08:32:37 +0200 Subject: [PATCH 5/6] drop duplicated disko-install definition --- disko-install.nix | 38 --------------------------------- tests/disko-install/default.nix | 6 +++--- 2 files changed, 3 insertions(+), 41 deletions(-) delete mode 100644 disko-install.nix diff --git a/disko-install.nix b/disko-install.nix deleted file mode 100644 index dbf124e..0000000 --- a/disko-install.nix +++ /dev/null @@ -1,38 +0,0 @@ -{ - stdenvNoCC, - makeWrapper, - lib, - coreutils, - xcp, - nixos-install-tools, -}: - -stdenvNoCC.mkDerivation { - name = "disko-install"; - src = ./.; - nativeBuildInputs = [ makeWrapper ]; - installPhase = '' - mkdir -p $out/bin $out/share/disko - cp -r install-cli.nix $out/share/disko - sed \ - -e "s|libexec_dir=\".*\"|libexec_dir=\"$out/share/disko\"|" \ - -e "s|#!/usr/bin/env.*|#!/usr/bin/env bash|" \ - disko-install > $out/bin/disko-install - chmod 755 $out/bin/disko-install - wrapProgram $out/bin/disko-install \ - --prefix PATH : "${ - lib.makeBinPath [ - coreutils - xcp - nixos-install-tools - ] - }" - ''; - meta = with lib; { - description = "Disko and nixos-install in one command"; - homepage = "https://github.com/nix-community/disko"; - license = licenses.mit; - maintainers = with maintainers; [ lassulus ]; - platforms = platforms.linux; - }; -} diff --git a/tests/disko-install/default.nix b/tests/disko-install/default.nix index ac85b66..eabb7b7 100644 --- a/tests/disko-install/default.nix +++ b/tests/disko-install/default.nix @@ -1,6 +1,6 @@ { pkgs ? import { }, self }: let - disko-install = pkgs.callPackage ../../disko-install.nix { }; + disko = pkgs.callPackage ../../package.nix { }; dependencies = [ self.nixosConfigurations.testmachine.pkgs.stdenv.drvPath @@ -37,9 +37,9 @@ pkgs.nixosTest { permission = machine.succeed("stat -c %a /tmp/age.key").strip() assert permission == "600", f"expected permission 600 on /tmp/age.key, got {permission}" - machine.succeed("${disko-install}/bin/disko-install --disk main /dev/vdb --extra-files /tmp/age.key /var/lib/secrets/age.key --flake ${../..}#testmachine") + machine.succeed("${disko}/bin/disko-install --disk main /dev/vdb --extra-files /tmp/age.key /var/lib/secrets/age.key --flake ${../..}#testmachine") # test idempotency - machine.succeed("${disko-install}/bin/disko-install --mode mount --disk main /dev/vdb --flake ${../..}#testmachine") + machine.succeed("${disko}/bin/disko-install --mode mount --disk main /dev/vdb --flake ${../..}#testmachine") machine.shutdown() new_machine = create_test_machine(oldmachine=machine, args={ "name": "after_install" }) From 718565dfbb8e43fd6d4d52416ffc90e3a1326836 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Tue, 7 May 2024 08:32:47 +0200 Subject: [PATCH 6/6] drop dependency on xcp It's actually not faster in combination with xargs... --- disko-install | 8 ++------ lib/make-disk-image.nix | 3 +-- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/disko-install b/disko-install index 19a13bc..e2b67c3 100755 --- a/disko-install +++ b/disko-install @@ -235,12 +235,8 @@ main() { if [[ ! -d "${mountPoint}/nix/store" ]]; then export NIX_STATE_DIR=${mountPoint}/nix/var/nix echo "Copying store paths" >&2 - - if [ -t 1 ]; then - xargs -I% xcp --recursive % "${mountPoint}/nix/store" < "${closure_info}/store-paths" - else - xargs -I% xcp --recursive --no-progress % "${mountPoint}/nix/store" < "${closure_info}/store-paths" - fi + mkdir -p "${mountPoint}/nix/store" + xargs cp --recursive --target "${mountPoint}/nix/store" < "${closure_info}/store-paths" echo "Loading nix database" >&2 nix-store --load-db < "${closure_info}/registration" unset NIX_STATE_DIR diff --git a/lib/make-disk-image.nix b/lib/make-disk-image.nix index c9b42f9..26f0d51 100644 --- a/lib/make-disk-image.nix +++ b/lib/make-disk-image.nix @@ -28,7 +28,6 @@ let systemdMinimal nix util-linux - xcp ] ++ nixosConfig.config.disko.extraDependencies; preVM = '' ${lib.concatMapStringsSep "\n" (disk: "truncate -s ${disk.imageSize} ${disk.name}.raw") (lib.attrValues nixosConfig.config.disko.devices.disk)} @@ -68,7 +67,7 @@ let # We copy files with cp because `nix copy` seems to have a large memory leak mkdir -p ${systemToInstall.config.disko.rootMountPoint}/nix/store - xargs -I % xcp --recursive % ${systemToInstall.config.disko.rootMountPoint}/nix/store < ${closureInfo}/store-paths + xargs cp --recursive --target ${systemToInstall.config.disko.rootMountPoint}/nix/store < ${closureInfo}/store-paths ${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 "" umount -Rv ${systemToInstall.config.disko.rootMountPoint}