-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
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
nixos/nixpkgs: add option to specify Nixpkgs source path #373201
base: master
Are you sure you want to change the base?
Conversation
@patch(get_qualified_name(nr.process.subprocess.run), autospec=True) | ||
def test_build_attr_from_arg(mock_run: Any, tmp_path: Path) -> None: | ||
nixpkgs_path = tmp_path / "nixpkgs" | ||
mock_run.side_effect = [ | ||
# with config.nixpkgs.source | ||
subprocess.CompletedProcess([], 0, f"\"{nixpkgs_path}\"\n"), | ||
# without config.nixpkgs.source | ||
subprocess.CompletedProcess([], 1, ""), | ||
] | ||
assert m.BuildAttr.from_arg(None, None) == m.BuildAttr(str(nixpkgs_path / "nixos"), None) | ||
assert m.BuildAttr.from_arg(None, None) == m.BuildAttr("<nixpkgs/nixos>", None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is better to do a scoped patch here instead:
@patch(get_qualified_name(nr.process.subprocess.run), autospec=True) | |
def test_build_attr_from_arg(mock_run: Any, tmp_path: Path) -> None: | |
nixpkgs_path = tmp_path / "nixpkgs" | |
mock_run.side_effect = [ | |
# with config.nixpkgs.source | |
subprocess.CompletedProcess([], 0, f"\"{nixpkgs_path}\"\n"), | |
# without config.nixpkgs.source | |
subprocess.CompletedProcess([], 1, ""), | |
] | |
assert m.BuildAttr.from_arg(None, None) == m.BuildAttr(str(nixpkgs_path / "nixos"), None) | |
assert m.BuildAttr.from_arg(None, None) == m.BuildAttr("<nixpkgs/nixos>", None) | |
def test_build_attr_from_arg(tmp_path: Path) -> None: | |
nixpkgs_path = tmp_path / "nixpkgs" | |
with patch(get_qualified_name(nr.process.subprocess.run), autospec=True, return_value=subprocess.CompletedProcess([], 0, f"\"{nixpkgs_path}\"\n")): | |
assert m.BuildAttr.from_arg(None, None) == m.BuildAttr(str(nixpkgs_path / "nixos"), None) | |
with patch(get_qualified_name(nr.process.subprocess.run), autospec=True, return_value=subprocess.CompletedProcess([], 1, "")): | |
assert m.BuildAttr.from_arg(None, None) == m.BuildAttr("<nixpkgs/nixos>", None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't comment too much about the change itself since I use Flakes, but at least the nixos-rebuild-ng
changes looks good to me.
Left a comment about the tests mocks but this is not a merge blocker.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, nixpkgs.source
option in nixos is an interesting approach to pin nixpkgs for a given nixos configuration. I found several issues with your implementation of it.
I cannot review changes in nixos-rebuild-ng because I am not familiar with its code.
in nixos-rebuild.sh, there is also one more instance of getting nixpkgs from nix path in line 646 (after changes). I think you forgot about that occurence.
@@ -157,6 +157,30 @@ in | |||
''; | |||
}; | |||
|
|||
source = lib.mkOption { | |||
type = lib.types.str; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
current type allows to point to a path outside of the nix store.
I don't think this was intended to allow out of store paths or any string here. Consider using lib.types.derivation
, which checks if the path, or string, is a store path.
@@ -157,6 +157,30 @@ in | |||
''; | |||
}; | |||
|
|||
source = lib.mkOption { | |||
type = lib.types.str; | |||
defaultText = "<nixpkgs>"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is not sufficiently described for/if flakes are used. I would remove defaultText
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uhm, I'm actually not sure what happens if you use flakes. I guess it will be completely ignored.
description = '' | ||
If set, the path to the Nixpkgs source that will be used by the | ||
`nixos-rebuild` tool to build the NixOS system, otherwise the default | ||
is to use the `<nixpkgs>` channel. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is not sufficiently described for/if flakes are used. Consider adding what the default is, when flakes are used.
is to use the `<nixpkgs>` channel. | |
is to use the nixpkgs NIX_PATH, or the flake input from where you're | |
using 'lib.nixosSystem'. |
# Link the Nixpkgs source to the system closure | ||
system.extraSystemBuilderCmds = lib.mkIf ( | ||
config.nixpkgs ? source | ||
) "ln -s ${config.nixpkgs.source} $out/nixpkgs"; | ||
|
||
# Set nixpkgs in NIX_PATH if nix-channel is disabled | ||
nix.nixPath = lib.optional ( | ||
config.nixpkgs ? source && !config.nix.channel.enable | ||
) "nixpkgs=/run/current-system/nixpkgs"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it is necessary to link nixpkgs to system derivation. You can directly use config.nixpkgs.source
in config.nix.nixPath
option definition.
What if I don't want to have nixpkgs in nixPath? there is no enable/disable option for that.
config.nixpkgs ? source
is not how you check if option is set or not, use options.nixpkgs.source.isDefined
# Link the Nixpkgs source to the system closure | |
system.extraSystemBuilderCmds = lib.mkIf ( | |
config.nixpkgs ? source | |
) "ln -s ${config.nixpkgs.source} $out/nixpkgs"; | |
# Set nixpkgs in NIX_PATH if nix-channel is disabled | |
nix.nixPath = lib.optional ( | |
config.nixpkgs ? source && !config.nix.channel.enable | |
) "nixpkgs=/run/current-system/nixpkgs"; | |
# Set nixpkgs in NIX_PATH if nix-channel is disabled | |
nix.nixPath = lib.optional ( | |
options.nixpkgs.source.isDefined && !config.nix.channel.enable | |
) "nixpkgs=${config.nixpkgs.source}"; |
@@ -489,7 +489,7 @@ if [[ -z $_NIXOS_REBUILD_REEXEC && -n $canRun && -z $fast ]]; then | |||
p=$(runCmd nix-build --no-out-link $buildFile -A "${attr:+$attr.}config.system.build.nixos-rebuild" "${extraBuildFlags[@]}") | |||
SHOULD_REEXEC=1 | |||
elif [[ -z $flake ]]; then | |||
if p=$(runCmd nix-build --no-out-link --expr 'with import <nixpkgs/nixos> {}; config.system.build.nixos-rebuild' "${extraBuildFlags[@]}"); then | |||
if p=$(runCmd nix-build --no-out-link --expr 'with import "$nixpkgsPath/nixos" {}; config.system.build.nixos-rebuild' "${extraBuildFlags[@]}"); then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$nixpkgsPath
used here is set from an environment variable, because function that set that variable is ran after this line.
this is also true for $nixpkgsPath
in line 579, 580, 582 (after changes).
@@ -604,6 +621,8 @@ getVersion() { | |||
} | |||
|
|||
|
|||
getNixpkgsPath |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this function should be ran at least before the first use of $nixpkgsPath
. Consider moving this and the function itself at least before the first use in line 492
# Get NixOS configuration | ||
NIXOS_CONFIG=${NIXOS_CONFIG:-$(runCmd nix-instantiate --find-file nixos-config)} | ||
if [[ -d $NIXOS_CONFIG ]]; then | ||
NIXOS_CONFIG=$NIXOS_CONFIG/default.nix | ||
fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it is necessary to move that code from line 517 (before changes) here.
What is the purpose of moving it?
I just noticed that there is option |
bd5f609
to
e2076cf
Compare
Thank you for reviewing!
Yes, I missed it because I was looking only for the angle brackets syntax. |
A non-breaking and (IMHO) simpler alternative to #333788 for setting the Nixpkgs versions declaratively from configuration.nix.
Things done
nix.conf
? (See Nix manual)sandbox = relaxed
sandbox = true
nixos-rebuild
,nixos-rebuild-ng
)Add a 👍 reaction to pull requests you find important.