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

All 4 top-level flake attributes should be exposed #10210

Open
sielicki opened this issue Mar 10, 2024 · 1 comment
Open

All 4 top-level flake attributes should be exposed #10210

sielicki opened this issue Mar 10, 2024 · 1 comment
Labels
feature Feature request or proposal flakes

Comments

@sielicki
Copy link
Contributor

sielicki commented Mar 10, 2024

Is your feature request related to a problem? Please describe.

I wanted to be able to generically refer to nixConfig settings in my flake outputs, eg:

❯ cat flake.nix
{
  nixConfig = {
    extra-substituters = [ "https://nix-community.cachix.org" ];
    extra-trusted-public-keys = [ "nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs=" ];
  };

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs";
  };

  outputs = { self, ... }@inputs: {
    nixosConfigurations.machine = inputs.nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      specialArgs = { inherit inputs; };
      modules = [
        ({ inputs, ... }: {
          nix.settings = {
            substituters = inputs.self.nixConfig.extra-substituters;
            trusted-public-keys = inputs.self.nixConfig.extra-trusted-public-keys;
          };
         })
      ];
    };
  };
}

example on  master
❯ nix eval .#nixosConfigurations.machine.config.nix.settings.substituters
error:
       … while calling the 'head' builtin

         at /nix/store/n6lhdmjihwplszgvfvnlsc38yfkxf7jv-source/lib/attrsets.nix:967:11:

          966|         || pred here (elemAt values 1) (head values) then
          967|           head values
             |           ^
          968|         else

       … while evaluating the attribute 'value'

         at /nix/store/n6lhdmjihwplszgvfvnlsc38yfkxf7jv-source/lib/modules.nix:809:9:

          808|     in warnDeprecation opt //
          809|       { value = builtins.addErrorContext "while evaluating the option `${showOption loc}':" value;
             |         ^
          810|         inherit (res.defsFinal') highestPrio;

       (stack trace truncated; use '--show-trace' to show the full trace)

       error: attribute 'nixConfig' missing

       at /nix/store/ry1yd7s8g99115ixcbh407ag4r39j03n-source/flake.nix:18:28:

           17|           nix.settings = {
           18|             substituters = inputs.self.nixConfig.extra-substituters;
             |                            ^
           19|             trusted-public-keys = inputs.self.nixConfig.extra-trusted-public-keys;

This can be worked around locally by wrapping the entire flake in rec, then injecting it:

rec {
  nixConfig = {
    extra-substituters = [ "https://nix-community.cachix.org" ];
    extra-trusted-public-keys = [ "nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs=" ];
  };

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs";
  };

  outputs = { self, ... }@inputs':
    let
      inputs = (inputs' // { self = inputs'.self // { inherit nixConfig; }; });
    in
    {
      nixosConfigurations.machine = inputs.nixpkgs.lib.nixosSystem {
        system = "x86_64-linux";
        specialArgs = { inherit inputs; };
        modules = [
          ({ inputs, ... }: {
            nix.settings = {
              substituters = inputs.self.nixConfig.extra-substituters;
              trusted-public-keys = inputs.self.nixConfig.extra-trusted-public-keys;
            };
          })
        ];
      };
    };
}
❯ nix eval .#nixosConfigurations.machine.config.nix.settings.substituters
warning: Git tree '/private/tmp/example' is dirty
[ "https://nix-community.cachix.org" "https://cache.nixos.org/" ]

This workaround is not so gross in the grand scheme of things, but it would not help in the case where a user wants to eg: consume and aggregate nixConfig settings from a set of input flakes.

Describe the solution you'd like

I don't see any reason why description or nixConfig should be any different than inputs and outputs -- call-flake.nix should make them available.

Describe alternatives you've considered

Described above.

Priorities

Add 👍 to issues you find important.

@sielicki sielicki added the feature Feature request or proposal label Mar 10, 2024
@roberth
Copy link
Member

roberth commented Mar 22, 2024

These could also be passed to the outputs function. That way it's easily available to functions that help with constructing flakes, such as those in flake-utils or flake-parts. Adding an argument requires a bit of care, not to break more flakes than necessary - this part could be done in/after

Also support for rec feels a bit accidental, but I don't see any harm in keeping it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Feature request or proposal flakes
Projects
None yet
Development

No branches or pull requests

2 participants