mirror of
https://github.com/NixOS/nix
synced 2025-06-24 22:11:15 +02:00
This is useful for frameworks (such as flake-parts) to avoid unnecessary evaluation. See discussion here: https://github.com/hercules-ci/flake-parts/issues/288#issuecomment-2912459614 While digging into this, I discovered that `nix fmt` already handles `null` formatters identically to undefined formatters. I added a couple of tests to demonstrate this behavior. `nix flake show` needs some reworking to avoid crashing with a "error: expected a derivation" when it encounters a `null` formatter or package. My changes here avoid the crash, but has some cosmetic issues, which is why I've labeled this PR as a draft. Cosmetic issues =============== With the following `flake.nix`: ```nix { outputs = _: { packages.x86_64-linux.default = null; }; } ``` `nix flake show` shows a weird empty system: ``` $ nix flake show path:/tmp/tmp.uL0iGuNwXB?lastModified=1748472558&narHash=sha256-tC%2BhdXAyoeFvWHNllJbois8X%2B7wpZ6CJzEzbcaGQxtM%3D └───packages └───x86_64-linux ``` Similarly, `nix flake show --json` includes an empty object: ``` $ nix flake show --json { "packages": { "x86_64-linux": { "default": null } } } ``` `nix build` crashes: ``` $ nix build .#default error: expected flake output attribute 'packages.x86_64-linux.default' to be a derivation or path but found null: null ```
135 lines
3 KiB
Bash
135 lines
3 KiB
Bash
# shellcheck shell=bash
|
|
|
|
source ../common.sh
|
|
|
|
# shellcheck disable=SC2034 # this variable is used by tests that source this file
|
|
registry=$TEST_ROOT/registry.json
|
|
|
|
writeSimpleFlake() {
|
|
local flakeDir="$1"
|
|
cat > "$flakeDir/flake.nix" <<EOF
|
|
{
|
|
description = "Bla bla";
|
|
|
|
outputs = inputs: rec {
|
|
packages.$system = rec {
|
|
foo = import ./simple.nix;
|
|
fooScript = (import ./shell.nix {}).foo;
|
|
default = foo;
|
|
noPackage = null;
|
|
};
|
|
packages.someOtherSystem = rec {
|
|
foo = import ./simple.nix;
|
|
default = foo;
|
|
};
|
|
|
|
# To test "nix flake init".
|
|
legacyPackages.$system.hello = import ./simple.nix;
|
|
|
|
parent = builtins.dirOf ./.;
|
|
|
|
baseName = builtins.baseNameOf ./.;
|
|
|
|
root = ./.;
|
|
};
|
|
}
|
|
EOF
|
|
|
|
cp ../simple.nix ../shell.nix ../simple.builder.sh "${config_nix}" "$flakeDir/"
|
|
}
|
|
|
|
createSimpleGitFlake() {
|
|
requireGit
|
|
local flakeDir="$1"
|
|
writeSimpleFlake "$flakeDir"
|
|
git -C "$flakeDir" add flake.nix simple.nix shell.nix simple.builder.sh config.nix
|
|
git -C "$flakeDir" commit -m 'Initial'
|
|
}
|
|
|
|
# Create a simple Git flake and add it to the registry as "flake1".
|
|
createFlake1() {
|
|
flake1Dir="$TEST_ROOT/flake1"
|
|
createGitRepo "$flake1Dir" ""
|
|
createSimpleGitFlake "$flake1Dir"
|
|
nix registry add --registry "$registry" flake1 "git+file://$flake1Dir"
|
|
}
|
|
|
|
createFlake2() {
|
|
flake2Dir="$TEST_ROOT/flake 2"
|
|
percentEncodedFlake2Dir="$TEST_ROOT/flake%202"
|
|
|
|
# Give one repo a non-main initial branch.
|
|
createGitRepo "$flake2Dir" "--initial-branch=main"
|
|
|
|
cat > "$flake2Dir/flake.nix" <<EOF
|
|
{
|
|
description = "Fnord";
|
|
|
|
outputs = { self, flake1 }: rec {
|
|
packages.$system.bar = flake1.packages.$system.foo;
|
|
};
|
|
}
|
|
EOF
|
|
|
|
git -C "$flake2Dir" add flake.nix
|
|
git -C "$flake2Dir" commit -m 'Initial'
|
|
|
|
nix registry add --registry "$registry" flake2 "git+file://$percentEncodedFlake2Dir"
|
|
}
|
|
|
|
writeDependentFlake() {
|
|
local flakeDir="$1"
|
|
cat > "$flakeDir/flake.nix" <<EOF
|
|
{
|
|
outputs = { self, flake1 }: {
|
|
packages.$system.default = flake1.packages.$system.default;
|
|
expr = assert builtins.pathExists ./flake.lock; 123;
|
|
};
|
|
}
|
|
EOF
|
|
}
|
|
|
|
writeIfdFlake() {
|
|
local flakeDir="$1"
|
|
cat > "$flakeDir/flake.nix" <<EOF
|
|
{
|
|
outputs = { self }: {
|
|
packages.$system.default = import ./ifd.nix;
|
|
};
|
|
}
|
|
EOF
|
|
|
|
cp -n ../ifd.nix ../dependencies.nix ../dependencies.builder0.sh "${config_nix}" "$flakeDir/"
|
|
}
|
|
|
|
writeTrivialFlake() {
|
|
local flakeDir="$1"
|
|
cat > "$flakeDir/flake.nix" <<EOF
|
|
{
|
|
outputs = { self }: {
|
|
expr = 123;
|
|
};
|
|
}
|
|
EOF
|
|
}
|
|
|
|
initGitRepo() {
|
|
local repo="$1"
|
|
local extraArgs="${2-}"
|
|
|
|
# shellcheck disable=SC2086 # word splitting of extraArgs is intended
|
|
git -C "$repo" init $extraArgs
|
|
git -C "$repo" config user.email "foobar@example.com"
|
|
git -C "$repo" config user.name "Foobar"
|
|
}
|
|
|
|
createGitRepo() {
|
|
local repo="$1"
|
|
local extraArgs="${2-}"
|
|
|
|
rm -rf "$repo" "$repo".tmp
|
|
mkdir -p "$repo"
|
|
|
|
# shellcheck disable=SC2086 # word splitting of extraArgs is intended
|
|
initGitRepo "$repo" $extraArgs
|
|
}
|