From e5ff78c28f894b6148a0b1644d6ea8c76391ff7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Tue, 17 Jun 2025 11:20:43 +0200 Subject: [PATCH] Apply nix fmt on setupNixInNixPython-only --- .../default.nix | 7 +- pkgs/testing/flake-module.nix | 108 ++++++++++++------ 2 files changed, 80 insertions(+), 35 deletions(-) diff --git a/checks/dummy-inventory-test-from-flake/default.nix b/checks/dummy-inventory-test-from-flake/default.nix index 8e2d4555a..8a94a37bd 100644 --- a/checks/dummy-inventory-test-from-flake/default.nix +++ b/checks/dummy-inventory-test-from-flake/default.nix @@ -22,10 +22,15 @@ nixosLib.runTest ( clan.test.fromFlake = ./.; + extraPythonPackages = _p: [ + clan-core.legacyPackages.${hostPkgs.system}.setupNixInNixPythonPackage + ]; + testScript = { nodes, ... }: '' - ${clan-core.legacyPackages.${hostPkgs.system}.setupNixInNixPython} + from setup_nix_in_nix import setup_nix_in_nix # type: ignore[import-untyped] + setup_nix_in_nix() def run_clan(cmd: list[str], **kwargs) -> str: import subprocess diff --git a/pkgs/testing/flake-module.nix b/pkgs/testing/flake-module.nix index 4c6b1de41..51c39729a 100644 --- a/pkgs/testing/flake-module.nix +++ b/pkgs/testing/flake-module.nix @@ -1,37 +1,77 @@ { - perSystem = { - legacyPackages = { - setupNixInNix = '' - export HOME=$TMPDIR - export NIX_STATE_DIR=$TMPDIR/nix - export NIX_CONF_DIR=$TMPDIR/etc - export IN_NIX_SANDBOX=1 - export CLAN_TEST_STORE=$TMPDIR/store - # required to prevent concurrent 'nix flake lock' operations - export LOCK_NIX=$TMPDIR/nix_lock - mkdir -p "$CLAN_TEST_STORE/nix/store" - mkdir -p "$CLAN_TEST_STORE/nix/var/nix/gcroots" - if [[ -n "''${closureInfo-}" ]]; then - xargs cp --recursive --target "$CLAN_TEST_STORE/nix/store" < "$closureInfo/store-paths" - nix-store --load-db --store "$CLAN_TEST_STORE" < "$closureInfo/registration" - fi - ''; - setupNixInNixPython = '' - from os import environ - import subprocess - from pathlib import Path - environ['HOME'] = environ['TMPDIR'] - environ['NIX_STATE_DIR'] = environ['TMPDIR'] + '/nix' - environ['NIX_CONF_DIR'] = environ['TMPDIR'] + '/etc' - environ['IN_NIX_SANDBOX'] = '1' - environ['CLAN_TEST_STORE'] = environ['TMPDIR'] + '/store' - environ['LOCK_NIX'] = environ['TMPDIR'] + '/nix_lock' - Path(environ['CLAN_TEST_STORE'] + '/nix/store').mkdir(parents=True, exist_ok=True) - Path(environ['CLAN_TEST_STORE'] + '/nix/var/nix/gcroots').mkdir(parents=True, exist_ok=True) - if 'closureInfo' in environ: - subprocess.run(['cp', '--recursive', '--target', environ['CLAN_TEST_STORE'] + '/nix/store'] + environ['closureInfo'].split(), check=True) - subprocess.run(['nix-store', '--load-db', '--store', environ['CLAN_TEST_STORE']] + ['<', environ['closureInfo'] + '/registration'], shell=True, check=True) - ''; + perSystem = + { pkgs, ... }: + { + legacyPackages = { + setupNixInNix = '' + set -xeu -o pipefail + export HOME=$TMPDIR + export NIX_STATE_DIR=$TMPDIR/nix + export NIX_CONF_DIR=$TMPDIR/etc + export IN_NIX_SANDBOX=1 + export CLAN_TEST_STORE=$TMPDIR/store + # required to prevent concurrent 'nix flake lock' operations + export LOCK_NIX=$TMPDIR/nix_lock + mkdir -p "$CLAN_TEST_STORE/nix/store" + mkdir -p "$CLAN_TEST_STORE/nix/var/nix/gcroots" + if [[ -n "''${closureInfo-}" ]]; then + # ${pkgs.findutils}/bin/xargs ${pkgs.xcp}/bin/xcp --recursive --target-directory "$CLAN_TEST_STORE/nix/store" < "$closureInfo/store-paths" + ${pkgs.findutils}/bin/xargs ${pkgs.coreutils}/bin/cp --recursive --target "$CLAN_TEST_STORE/nix/store" < "$closureInfo/store-paths" + ${pkgs.nix}/bin/nix-store --load-db --store "$CLAN_TEST_STORE" < "$closureInfo/registration" + fi + ''; + + setupNixInNixPythonPackage = pkgs.python3Packages.buildPythonPackage { + pname = "setup-nix-in-nix"; + version = "1.0.0"; + format = "other"; + + dontUnpack = true; + + installPhase = '' + mkdir -p $out/${pkgs.python3.sitePackages} + cat > $out/${pkgs.python3.sitePackages}/setup_nix_in_nix.py << 'EOF' + from os import environ + import subprocess + from pathlib import Path + + def setup_nix_in_nix(): + """Set up a Nix store inside the test environment.""" + environ['HOME'] = environ['TMPDIR'] + environ['NIX_STATE_DIR'] = environ['TMPDIR'] + '/nix' + environ['NIX_CONF_DIR'] = environ['TMPDIR'] + '/etc' + environ['IN_NIX_SANDBOX'] = '1' + environ['CLAN_TEST_STORE'] = environ['TMPDIR'] + '/store' + environ['LOCK_NIX'] = environ['TMPDIR'] + '/nix_lock' + + Path(environ['CLAN_TEST_STORE'] + '/nix/store').mkdir(parents=True, exist_ok=True) + Path(environ['CLAN_TEST_STORE'] + '/nix/var/nix/gcroots').mkdir(parents=True, exist_ok=True) + + if 'closureInfo' in environ: + # Read store paths from the closure info file + with open(environ['closureInfo'] + '/store-paths', 'r') as f: + store_paths = f.read().strip().split('\n') + + # Copy store paths using absolute path to cp + subprocess.run( + ['${pkgs.coreutils}/bin/cp', '--recursive', '--target', environ['CLAN_TEST_STORE'] + '/nix/store'] + store_paths, + check=True + ) + + # Load the nix database using absolute path to nix-store + with open(environ['closureInfo'] + '/registration', 'r') as f: + subprocess.run( + ['${pkgs.nix}/bin/nix-store', '--load-db', '--store', environ['CLAN_TEST_STORE']], + input=f.read(), + text=True, + check=True + ) + EOF + touch $out/${pkgs.python3.sitePackages}/py.typed + ''; + + doCheck = false; + }; + }; }; - }; }