Skip to content

Conversation

infinisil
Copy link
Member

Description of changes

(in addition to various minor improvements)

Previously, nixos-rebuild -A foo would build the default.nix in the current directory, while nixos-rebuild without -A would not use file-based building at all.

This difference compared to what nix-build would do is confusing. Furthermore, using default.nix is not great, because there's many of those files all around, and they're almost never for NixOS.

This PR fixes those two issues by:

  • Removing support for using default.nix as a default when using --attr, this is a breaking change, but support for this has not made it to a stable branch
  • Using the <nixos-system> entry in NIX_PATH as the default if it exists
  • Otherwise, use the system.nix file in the current directory or any of its parents, if it exists, as a default
  • Otherwise, use /etc/nixos/system.nix as a default

While system.nix could be conflated with the system Nixpkgs argument, there's no practical way for this to cause problems, because system is never specified in a file.

Having this finally allows using nixos-rebuild without any arguments and without any NIX_PATH entries, effectively a way to declaratively use NixOS without nix-channel!

I think it's important to get this for the next release, so that we don't bake in stability for the default.nix behavior.

Unfortunately I'm a bit low on time, but if @amozeo (who pioneered the file-based building with #320462) could take this PR to its conclusion I'd be very grateful!

Things done

  • Tests
  • Documentation

Add a 👍 reaction to pull requests you find important.

In bash it's convention for "" to represent false, and "1" true.
The `buildingAttribute` variable didn't honor that before.
Otherwise we'd run into trouble when a filename contained e.g. a space
To more accurately represent what it does
Opens the file that would be built from
Previously, `nixos-rebuild -A foo` would build the `default.nix` in the
current directory, while `nixos-rebuild` without `-A` would not use
file-based building at all.

This difference compared to what `nix-build` would do is confusing.

Furthermore, using `default.nix` is not great, because there's many of
those files all around, and they're almost never for NixOS.

This commit fixes those two issues by:
- Removing support for using `default.nix` as a default when using `--attr`
- If it exists, using the `<nixos-system>` entry in NIX_PATH as the default
- Otherwise, use the `system.nix` file in the current directory or any
  of its parents, if it exists, as a default
- Otherwise, use `/etc/nixos/system.nix` as a default

While `system.nix` could be conflated with the `system` Nixpkgs
argument, there's no practical way for this to cause problems, because
`system` is never specified in a file.

Having this finally allows using `nixos-rebuild` without any arguments
and without any `NIX_PATH` entries, effectively a way to declaratively
use NixOS from just /etc/nixos!
@infinisil infinisil added the 1.severity: significant Novel ideas, large API changes, notable refactorings, issues with RFC potential, etc. label Aug 10, 2024
@github-actions github-actions bot added the 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS label Aug 10, 2024
@ofborg ofborg bot added 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. labels Aug 10, 2024
@infinisil infinisil added this to the 24.11 milestone Aug 11, 2024
@amozeo
Copy link
Contributor

amozeo commented Aug 22, 2024

hey @infinisil, I wrote tests for configuration detection. I think you might be interested looking into it
it is available here: permalink, latest, permalink diff, latest diff

@infinisil
Copy link
Member Author

@amozeo Neat idea, it seems to work well with system.nix! Were you thinking of merging that idea into this PR? I'd prefer having smaller separate PRs that only focus on fixing one problem :)

@amozeo
Copy link
Contributor

amozeo commented Aug 25, 2024

@infinisil how I should split it then?

@infinisil
Copy link
Member Author

Honestly #327740 seemed fine to me. I guess it would be best to have this PR first though. I'm unfortunately a bit low on time right now, so I'd appreciate if you could help out!

In particular I think this PR should get done before the release in ~2 months, because I don't think the current behavior regarding default.nix is great, and we'd be locking that into stable 😓

@nyabinary
Copy link
Contributor

We should probably get this moving if we want to make it before next release right?

Comment on lines +396 to +399
# Hardcoded to /etc/nixos/system.nix
if [[ -f "/etc/nixos/system.nix" ]]; then
buildFile=/etc/nixos/system.nix
fi
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this path should be hardcoded in nixos-rebuild. Setup with channels doesn't have /etc/nixos/configuration.nix hardcoded and uses nixos-config in NIX_PATH instead to find its configuration there. If the /etc/nixos/system.nix must be hardcoded, it should be in NixOS modules instead, similar to channel setup.

Copy link
Contributor

@amozeo amozeo Oct 12, 2024

Choose a reason for hiding this comment

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

flake path /etc/nixos/flake.nix is hardcoded in nixos-rebuild, I don't think the issue that I brought up has any significance anymore

@amozeo
Copy link
Contributor

amozeo commented Oct 12, 2024

@infinisil I have force-updated the branch on my fork that I linked before.
permalink, latest, permalink diff, latest diff.
I want to make sure before I do something. Are you sure that I should make 4 separate pull requests for each feature that this batch of commits brings?
These pull requests would be named:

  • nixos-{rebuild, install}: Support system.nix by default and improve configuration detection
  • lib/version-info-fixup: create
  • nixos-generate-config: Introduce templates
  • nixos-rebuild: Add test for configuration detection

@nyabinary
Copy link
Contributor

@infinisil I have force-updated the branch on my fork that I linked before. permalink, latest, permalink diff, latest diff. I want to make sure before I do something. Are you sure that I should make 4 separate pull requests for each feature that this batch of commits brings? These pull requests would be named:

* nixos-{rebuild, install}: Support `system.nix` by default and improve configuration detection

* lib/version-info-fixup: create

* nixos-generate-config: Introduce templates

* nixos-rebuild: Add test for configuration detection

Doesn't hurt to open the PR right now as all of them together, and then you can split them later if required.

@nyabinary
Copy link
Contributor

Bump :P 24.11 is rapidly approaching

@emilazy
Copy link
Member

emilazy commented Oct 28, 2024

We’re in feature freeze, so this would need @RossComputerGuy’s sign‐off even if it wasn’t a draft.

Probably just removing the default.nix handling could be spun off into a separate PR.

@RossComputerGuy
Copy link
Member

Yeah, let's kill the default.nix post release. This should be fine.

@nyabinary
Copy link
Contributor

Yeah, let's kill the default.nix post release. This should be fine.

bump :P

@justinas
Copy link
Member

justinas commented Mar 9, 2025

My 2c as a big fan of "flake-less, channel-less" model: this is a great change.

Would we like to default to building /etc/nixos/system.nix if it exists, over building configuration.nix, similarly how it is done for flakes? This would pretty much non-breaking if the priority remains: build flake.nix if it exists, build system.nix if it exists, else fall back on configuration.nix + <nixpkgs/nixos>. This approach could still break for people who happen to be using /etc/nixos/configuration.nix, and have an unrelated file named /etc/nixos/system.nix. Should be fine if documented in breaking changes?

Are there any plans to make system.nix and the plumbing such as lib/eval-config.nix more of first-class citizens? Right now there's quite a big disconnect in how NixOS is built using flakes (explicit call to lib.nixosSystem - another variation of basically lib/eval-config.nix), and how it is usually built in a flake-less way (/etc/nixos/configuration.nix and <nixpkgs/nixos> inferred from the environment, these are sort of "implementation details" that users often remain ignorant of).

In other words, do we see a future where perhaps nixos-generate-config also generates a /etc/nixos/system.nix with something like:

let
  nixpkgs = <nixpkgs>; # keeping compat with the "channelful" model by default
in
import "${nixpkgs}/nixos/lib/eval-config.nix" {
  system = null;
  modules = [
    ./configuration.nix
  ];
}

and this becomes a supported/recommended approach?

Let me know if this issue is not a place to discuss this, I'll make a new one. I'm just curious and excited about any ways to demystify the NixOS build process and decouple it somewhat from <nixpkgs/nixos> / <nixos-config> assumptions / implementation details in nixos-rebuild.

@amozeo
Copy link
Contributor

amozeo commented Mar 13, 2025

I created #389487 that addresses issues pointed out in this pull request.
For anyone subscribed to this pull request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.severity: significant Novel ideas, large API changes, notable refactorings, issues with RFC potential, etc. 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants