Skip to content

Use hostPlatform.system in nixos-generate-config#228133

Merged
roberth merged 1 commit intoNixOS:masterfrom
name-snrl:fix-nix-in-nixos-generate-config
Apr 26, 2023
Merged

Use hostPlatform.system in nixos-generate-config#228133
roberth merged 1 commit intoNixOS:masterfrom
name-snrl:fix-nix-in-nixos-generate-config

Conversation

@name-snrl
Copy link
Contributor

@name-snrl name-snrl commented Apr 25, 2023

Description of changes

Make nixos-generate-config respect nix.package option

Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 23.05 Release Notes (or backporting 22.11 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

@github-actions github-actions bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` labels Apr 25, 2023
@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. labels Apr 25, 2023
@name-snrl
Copy link
Contributor Author

@roberth @mweinelt @dasJ @Ma27 Could you review, please?

@roberth roberth changed the title Fix nix in nixos-generate-config Use nix.package in nixos-generate-config Apr 26, 2023
Copy link
Member

@roberth roberth left a comment

Choose a reason for hiding this comment

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

Looks ok.

I wonder if we should just put the value of stdenv.hostPlatform.system as a hardcoded value into the script instead. Asking nix itself seemed like the right thing, but I can't think of a realistic use case that needs to query nix at runtime.

@name-snrl
Copy link
Contributor Author

@roberth I'm not sure about new commit, but it's work for me.

@ofborg ofborg bot added 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. and removed 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. labels Apr 26, 2023
@roberth roberth changed the title Use nix.package in nixos-generate-config Use hostPlatform.system in nixos-generate-config Apr 26, 2023
@roberth
Copy link
Member

roberth commented Apr 26, 2023

Passes installer test.

@roberth roberth merged commit 87676d0 into NixOS:master Apr 26, 2023
@name-snrl name-snrl deleted the fix-nix-in-nixos-generate-config branch April 27, 2023 04:31
@roberth
Copy link
Member

roberth commented Apr 30, 2023

Thanks!

@tpwrules
Copy link
Contributor

tpwrules commented Jun 7, 2023

This breaks things for cross-compiled installer images.

@roberth
Copy link
Member

roberth commented Jun 7, 2023

@tpwrules what things? If you want the problem to be solved, you'll have to be more specific.

@tpwrules
Copy link
Contributor

tpwrules commented Jun 11, 2023

My apologies. This causes nixpkgs.hostPlatform to be set to the system type where the installer image was built instead of the system type where nixos-generate-config was run. Therefore, on a fresh install, nixpkgs is evaluated for the wrong system and the install fails.

I'm not sure whether reverting this commit is the right option or if there's a better source to fill in this field in a hard-coded manner.

@roberth
Copy link
Member

roberth commented Jun 11, 2023

@tpwrules The file containing the nixpkgs.hostPlatform definition is documented as follows

/etc/nixos/hardware-configuration.nix
This module sets NixOS configuration options based on your current hardware configuration.

My reasoning for using hostPlatform directly, is that nixos-generate-config is meant to run on the host* hardware.
So far so good, but how does nixos-generate-config even run on the final hardware? I've assumed that you would cross compile nixos-generate-config, because otherwise it can't run on the final hardware.

The previous behavior was to pull from builtins.currentSystem aka the system option in nix.conf or whatever Nix itself was built with. I would expect that value to coincide with hostPlatform as well. So I'm skeptical that reverting this PR would fix your problem.

if there's a better source to fill in this field in a hard-coded manner.

I'm not aware of a better source than eval-time hostPlatform which is generally equal to the runtime nix system on the host, including on a cross compiled installer image (I would surely expect).

You mention an installer image. Where did that image come from? Maybe something is wrong about the image?

Or maybe it should be made a bit more clear that you're not supposed to use a hardware-configuration.nix generated on one machine for the configuration of a different machine. It kind of seems like that's what you're doing, and it wouldn't work well for other reasons, like wrong file system labels and driver modules.

* (perhaps superfluously) host from the GNU terminology: build is where the build is performed, host is where the output of the build runs, target is irrelevant because a NixOS configuration is not a compiler we're compiling.

@tpwrules
Copy link
Contributor

tpwrules commented Jun 11, 2023

I publish my own cross-compiled installer images here. They are designed to be cross-compiled from x86_64-linux to aarch64-linux (or built natively on aarch64-linux). When the cross-compiled image is booted on the aarch64-linux device and nixos-generate-config is run from that image on that device, it puts nixpkgs.hostPlatform = "x86_64-linux"; into hardware-configuration.nix and the system does not build. It is absolutely not the case that hardware-configuration.nix is being moved between systems.

Using the linked commit on an x86_64-linux system:

$ nix eval .#installer-bootstrap.config.system.build.nixos-generate-config.stdenv.hostPlatform.system
"aarch64-linux"

$ nix eval .#installer-bootstrap.config.system.build.nixos-generate-config.system
"x86_64-linux"

$ nix build .#installer-bootstrap.config.system.build.nixos-generate-config

$ cat result/bin/nixos-generate-config | grep hostPlatform
push @attrs, "nixpkgs.hostPlatform = lib.mkDefault \"x86_64-linux\";";

You are right that hostPlatform is the right attribute and I did have a misconception about the terminology. But there is something wrong and the wrong hostPlatform is clearly being substituted even though the cross-compiled image builds and boots successfully and has for many months, including before this commit. It's not immediately clear to me what the issue is and if it's the fault of my configuration. Do you have any ideas?

@roberth
Copy link
Member

roberth commented Jun 11, 2023

#237218 should fix it. Could you give it a try?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-darwin: 1 This PR causes 1 package 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.

3 participants