Skip to content

WIP: Allow for null packages and formatters #13291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jfly
Copy link
Contributor

@jfly jfly commented May 28, 2025

This is useful for frameworks (such as flake-parts) to avoid unnecessary evaluation. See discussion here:
hercules-ci/flake-parts#288 (comment)

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:

{
  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

Motivation

Context


Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

This is useful for frameworks (such as flake-parts) to avoid unnecessary
evaluation. See discussion here:
hercules-ci/flake-parts#288 (comment)

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
```
@jfly jfly requested a review from roberth May 28, 2025 23:04
@github-actions github-actions bot added new-cli Relating to the "nix" command with-tests Issues related to testing. PRs with tests have some priority labels May 28, 2025
@@ -762,6 +763,14 @@ std::vector<Symbol> AttrCursor::getAttrs()
return attrs;
}

bool AttrCursor::isNull()
{
// <<< TODO: caching? >>>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's surprising, but yeah, null isn't supported yet.

enum AttrType could be extended.
Not sure if that should be considered a breaking schema change, but it'd be a safe choice to then also update the version in the file name "/eval-cache-v5" to v6.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll give that a shot! I see some type related checks in the other caching codepaths. Do I need to worry about the fact that the attribute I'm caching could change from a derivation to null and back?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-cli Relating to the "nix" command with-tests Issues related to testing. PRs with tests have some priority
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants