Skip to content
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

network: Add nixpkgs attribute for passing custom nixpkgs #1369

Merged
merged 2 commits into from Jul 10, 2020

Conversation

adisbladis
Copy link
Member

This is useful to pin nixpkgs without flakes.

@adisbladis adisbladis changed the title network: Add nixpkgs attribute for passing nixpkgs network: Add nixpkgs attribute for passing custom nixpkgs Jun 12, 2020
@cole-h
Copy link
Member

cole-h commented Jun 12, 2020

Is using this as simple as setting network.nixpkgs = sources.nixpkgs; or something?

Would be nice if there were some docs to accompany this describing how to use it -- feel free to take inspiration from my suggestion over at #1331.

@adisbladis
Copy link
Member Author

An example of how to use this is:

{
  network.description = "Test deployment";
  network.nixpkgs = import <nixpkgs> {};

  myhost = { config, lib, resources, ... }: {};
}

Copy link
Member

@cole-h cole-h left a comment

Choose a reason for hiding this comment

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

On this branch, if I set network.nixpkgs = import sources.nixpkgs { }; and I run NIX_PATH= nixops deploy --dry-activate, I get the following error:

error: file 'nixpkgs/nixos' was not found in the Nix search path (add it using $NIX_PATH or -I)
Traceback (most recent call last):
  File "/nix/store/50l5y9imknxf9hx58kmy46ffbfwhf32q-python3.7-nixops-1.8.0/bin/.nixops-wrapped", line 9, in <module>
    sys.exit(main())
  File "/nix/store/50l5y9imknxf9hx58kmy46ffbfwhf32q-python3.7-nixops-1.8.0/lib/python3.7/site-packages/nixops/__main__.py", line 710, in main
    args.op(args)
  File "/nix/store/50l5y9imknxf9hx58kmy46ffbfwhf32q-python3.7-nixops-1.8.0/lib/python3.7/site-packages/nixops/script_defs.py", line 638, in op_deploy
    max_concurrent_activate=args.max_concurrent_activate,
  File "/nix/store/50l5y9imknxf9hx58kmy46ffbfwhf32q-python3.7-nixops-1.8.0/lib/python3.7/site-packages/nixops/deployment.py", line 1418, in deploy
    self.run_with_notify("deploy", lambda: self._deploy(**kwargs))
  File "/nix/store/50l5y9imknxf9hx58kmy46ffbfwhf32q-python3.7-nixops-1.8.0/lib/python3.7/site-packages/nixops/deployment.py", line 1407, in run_with_notify
    f()
  File "/nix/store/50l5y9imknxf9hx58kmy46ffbfwhf32q-python3.7-nixops-1.8.0/lib/python3.7/site-packages/nixops/deployment.py", line 1418, in <lambda>
    self.run_with_notify("deploy", lambda: self._deploy(**kwargs))
  File "/nix/store/50l5y9imknxf9hx58kmy46ffbfwhf32q-python3.7-nixops-1.8.0/lib/python3.7/site-packages/nixops/deployment.py", line 1334, in _deploy
    dry_run=dry_run, repair=repair, include=include, exclude=exclude
  File "/nix/store/50l5y9imknxf9hx58kmy46ffbfwhf32q-python3.7-nixops-1.8.0/lib/python3.7/site-packages/nixops/deployment.py", line 761, in build_configs
    text=True,
  File "/nix/store/q732h09azy7lf0j30bnnhdl15p4rxpdy-python3-3.7.7/lib/python3.7/subprocess.py", line 411, in check_output
    **kwargs).stdout
  File "/nix/store/q732h09azy7lf0j30bnnhdl15p4rxpdy-python3-3.7.7/lib/python3.7/subprocess.py", line 512, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['nix-instantiate', '--find-file', 'nixpkgs/nixos', '-I', 'nixops=/nix/store/50l5y9imknxf9hx58kmy46ffbfwhf32q-python3.7-nixops-1.8.0/lib/python3.7/site-packages/nixops/../nix']' returned non-zero exit status 1.

This appears to stem from

["nix-instantiate", "--find-file", "nixpkgs/nixos"]
(as you'll probably notice in the traceback).

@cole-h
Copy link
Member

cole-h commented Jun 15, 2020

I fixed my previous issue with the following diff. However, note that it's very brittle -- it relies on self.nix_exprs[0] being the network file that has network.nixpkgs defined in order to get the nixpkgs path, in addition to preventing any other -I nixpkgs= flags from taking effect (because AFAICT the first of a flag wins over all the others). Now I can run NIX_PATH= nixops deploy --dry-activate and it works as expected! Another note is that NIX_PATH= nixops deploy without a network.nixpkgs defined fails as it did before.

diff --git a/nixops/deployment.py b/nixops/deployment.py
index b3ccb40f..bc389237 100644
--- a/nixops/deployment.py
+++ b/nixops/deployment.py
@@ -360,6 +360,18 @@ class Deployment:
             path for paths in get_plugin_manager().hook.nixexprs() for path in paths
         ]
 
+        nixpkgs_path = subprocess.run(
+            [
+                "nix-instantiate",
+                "--eval",
+                # FIXME: self.nix_exprs[0] is not guaranteed to always work
+                "-E" "(import {0}).network.nixpkgs.path".format(self.nix_exprs[0]),
+            ],
+            stdout=subprocess.PIPE,
+            stderr=subprocess.DEVNULL,
+            text=True,
+        ).stdout.rstrip()
+
         flags = (
             list(
                 itertools.chain(
@@ -371,6 +383,9 @@ class Deployment:
             )
             + self.extra_nix_flags
         )
+        flags.extend(
+            ["-I", "nixpkgs=" + nixpkgs_path] if nixpkgs_path is not "" else []
+        )
         flags.extend(["-I", "nixops=" + self.expr_path])
         return flags
 

@lovesegfault
Copy link
Member

Is anything blocking this?

Looking up NIX_PATH is no longer a viable solution as we have both
flakes & network.nixpkgs to describe how to find nixpkgs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants