diff --git a/nixos/modules/hardware/facter/debug.nix b/nixos/modules/hardware/facter/debug.nix new file mode 100644 index 000000000000..810a6abbcd9e --- /dev/null +++ b/nixos/modules/hardware/facter/debug.nix @@ -0,0 +1,81 @@ +{ + lib, + pkgs, + config, + extendModules, + ... +}: +{ + + options = { + + system.build = { + noFacter = lib.mkOption { + type = lib.types.unspecified; + description = "A version of the system closure with facter disabled"; + }; + }; + + hardware.facter.debug = { + nvd = lib.mkOption { + type = lib.types.package; + description = '' + A shell application which will produce an nvd diff of the system closure with and without facter enabled. + ''; + }; + nix-diff = lib.mkOption { + type = lib.types.package; + description = '' + A shell application which will produce a nix-diff of the system closure with and without facter enabled. + ''; + }; + }; + + }; + + config.system.build = { + noFacter = extendModules { + modules = [ + { + # we 'disable' facter by overriding the report and setting it to empty with one caveat: hostPlatform + config.hardware.facter.report = lib.mkForce { + system = config.nixpkgs.hostPlatform; + }; + } + ]; + }; + }; + + config.hardware.facter.debug = { + + nvd = pkgs.writeShellApplication { + name = "facter-nvd-diff"; + runtimeInputs = [ + config.nix.package + pkgs.nvd + ]; + text = '' + nvd diff \ + ${config.system.build.noFacter.config.system.build.toplevel} \ + ${config.system.build.toplevel} \ + "$@" + ''; + }; + + nix-diff = pkgs.writeShellApplication { + name = "facter-nix-diff"; + runtimeInputs = [ + config.nix.package + pkgs.nix-diff + ]; + text = '' + nix-diff \ + ${config.system.build.noFacter.config.system.build.toplevel} \ + ${config.system.build.toplevel} \ + "$@" + ''; + }; + + }; + +} diff --git a/nixos/modules/hardware/facter/default.nix b/nixos/modules/hardware/facter/default.nix index 12499b5ad31f..e1376769a333 100644 --- a/nixos/modules/hardware/facter/default.nix +++ b/nixos/modules/hardware/facter/default.nix @@ -7,6 +7,7 @@ imports = [ ./bluetooth.nix ./camera + ./debug.nix ./disk.nix ./fingerprint ./firmware.nix diff --git a/pkgs/by-name/ni/nixos-facter/package.nix b/pkgs/by-name/ni/nixos-facter/package.nix index bf5e2b4ecca6..7578c0ad78b0 100644 --- a/pkgs/by-name/ni/nixos-facter/package.nix +++ b/pkgs/by-name/ni/nixos-facter/package.nix @@ -1,6 +1,7 @@ { lib, buildGoModule, + callPackage, fetchFromGitHub, hwinfo, libusb1, @@ -64,6 +65,8 @@ buildGoModule rec { passthru.tests = { inherit (nixosTests) facter; + debug-nvd = callPackage ./test-debug-nvd.nix { }; + debug-nix-diff = nixosTests.facter.nodes.machine.hardware.facter.debug.nix-diff; }; meta = { diff --git a/pkgs/by-name/ni/nixos-facter/test-debug-nvd.nix b/pkgs/by-name/ni/nixos-facter/test-debug-nvd.nix new file mode 100644 index 000000000000..f6b8a8f90cd8 --- /dev/null +++ b/pkgs/by-name/ni/nixos-facter/test-debug-nvd.nix @@ -0,0 +1,52 @@ +{ + runCommand, + jq, + nix, + nixosTests, +}: +let + facterDebug = nixosTests.facter.nodes.machine.hardware.facter.debug; + withFacter = nixosTests.facter.nodes.machine.system.build.toplevel; + noFacter = nixosTests.facter.nodes.machine.system.build.noFacter.config.system.build.toplevel; +in +runCommand "facter-debug-nvd-test" + { + nativeBuildInputs = [ + facterDebug.nvd + jq + nix + ]; + __structuredAttrs = true; + unsafeDiscardReferences.out = true; + exportReferencesGraph.withFacter = [ withFacter ]; + exportReferencesGraph.noFacter = [ noFacter ]; + } + '' + # Set up nix environment with read-only store but writable state + mkdir -p "$TMPDIR/nix"/{var/nix,var/log/nix,etc} + + export NIX_REMOTE="" + export NIX_STATE_DIR="$TMPDIR/nix/var/nix" + export NIX_LOG_DIR="$TMPDIR/nix/var/log/nix" + export NIX_CONF_DIR="$TMPDIR/nix/etc" + cat > "$TMPDIR/nix/etc/nix.conf" < 0 then "\n" + (.references | join("\n")) else "" end)"' \ + < "$NIX_ATTRS_JSON_FILE" | nix-store --load-db + + # Run nvd and verify it produces output + facter-nvd-diff > nvd-output.txt + if [ ! -s nvd-output.txt ]; then + echo "facter-nvd-diff produced no output" + exit 1 + fi + cat nvd-output.txt + echo "nvd debug utility works correctly" > $out + ''