Can't use derivation from inputs in Nix flake

Hi, I’m new to Nix Flakes and want to import a package in my NixOS config. In order to do so, I created a Nix flake for my package shown below:

{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = import nixpkgs { inherit system; };
      in {
        packages = rec {
          mypkg = pkgs.stdenv.mkDerivation { ... };
          default = mypkg;
        };
      });
}

When I tried to import the derivation in my NixOS configuration, I tried:

{
  inputs = {
    mypkg.url = "path:/path/to/mypkg";
  };

  outputs = { nixpkgs, ... }@inputs: {
    nixosConfigurations = {
      legion = nixpkgs.lib.nixosSystem rec {
        system = "x86_64-linux";
        # pass mypkgs to modules
        specialArgs = {
          mypkgs = {
            mypkg = inputs.mypkg.packages.default.${system};
          };
        };
        modules = [
          ./configuration.nix
        ];
      };
    };
  };
}

But I encountered the error error: attribute 'packages' missing at the mypkg line, and I’m confused how to import it correctly. I searched a lot but still didn’t find any solution. I would appreciate it if anyone could help fix this issue.

Thanks

I think it should be inputs.mypkg.packages.${system}.default (system first) but that wouldn’t explain your error.

I like to open up nix repl, load the flake with :lf . (other flake urls work, but usually I’m j the directory) and then work through tab completion, starting with inputs.mypkg.

The less-interactive way would be to flake show path:/path/to/mypkg

Hope that helps somewhat, I’m on my phone so the commands might be a little off

Yes I tried inputs.mypkg.packages.${system}.default but the result is the same. The result of nix flake show is shown below:

path:....
└───packages
    ├───aarch64-darwin
    │   ├───default omitted (use '--all-systems' to show)
    │   └───mypkg omitted (use '--all-systems' to show)
    ├───aarch64-linux
    │   ├───default omitted (use '--all-systems' to show)
    │   └───mypkg omitted (use '--all-systems' to show)
    ├───x86_64-darwin
    │   ├───default omitted (use '--all-systems' to show)
    │   └───mypkg omitted (use '--all-systems' to show)
    └───x86_64-linux
        ├───default: package 'mypkg'
        └───mypkg: package 'mypkg'

It looks good to me though. BTW, I’m using nixos-rebuild switch to deploy my configuration, not sure if that’s relevant to this error.

Here you are adding an argument to your modules, named mypkgs, which otself will be a set with a single child mypkg, which points to an inexisting attrinbute in your flake.

How and where are you actually using this additional argument in your modules?

Actually I’m trying to import an emacs package. So I used the package within the home-manager config, which is imported as the same level as configuration.nix:

{ pkgs, mypkgs, ... }:
{
  home-manager.users."${user}" = { lib, ... }: {
    programs.emacs = {
      enable = true;
      package = pkgs.emacs29;
      # emacs packages
      extraPackages = epkgs: [
        mypkgs.mypkg
      ];
    };
  };
}

And the error does point to the line where I access the packages attribute:

error: attribute 'packages' missing

at /nix/store/akf3s0jinsnggpk0z4zm9qpgf8d7d2bb-source/flake.nix:21:23:
       20|           mypkgs = {
       21|             mypkg = inputs.mypkg.packages.default.${system};

So I think it shouldn’t be caused by how I use the pkg right?

In that case it should be inputs.mypkg.packages.${system}.default.

If that doesn’t fix it, please show the full error, or even better the flakes repositories.

The full error is shown below:

building the system configuration...
error:
       … while calling anonymous lambda

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/attrsets.nix:780:24:

          779|     let f = attrPath:
          780|       zipAttrsWith (n: values:
             |                        ^
          781|         let here = attrPath ++ [n]; in

       … while calling 'g'

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/attrsets.nix:599:19:

          598|           g =
          599|             name: value:
             |                   ^
          600|             if isAttrs value && cond value

       … from call site

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/attrsets.nix:602:20:

          601|               then recurse (path ++ [name]) value
          602|               else f (path ++ [name]) value;
             |                    ^
          603|         in mapAttrs g;

       … while calling anonymous lambda

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/modules.nix:242:72:

          241|           # For definitions that have an associated option
          242|           declaredConfig = mapAttrsRecursiveCond (v: ! isOption v) (_: v: v.value) options;
             |                                                                        ^
          243|

       … while evaluating the option `system.build.toplevel':

       … while calling anonymous lambda

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/modules.nix:783:28:

          782|         # Process mkMerge and mkIf properties.
          783|         defs' = concatMap (m:
             |                            ^
          784|           map (value: { inherit (m) file; inherit value; }) (builtins.addErrorContext "while evaluating definitions from `${m.file}':" (dischargeProperties m.value))

       … while evaluating definitions from `/nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/nixos/modules/system/activation/top-level.nix':

       … from call site

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/modules.nix:784:137:

          783|         defs' = concatMap (m:
          784|           map (value: { inherit (m) file; inherit value; }) (builtins.addErrorContext "while evaluating definitions from `${m.file}':" (dischargeProperties m.value))
             |                                                                                                                                         ^
          785|         ) defs;

       … while calling 'dischargeProperties'

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/modules.nix:855:25:

          854|   */
          855|   dischargeProperties = def:
             |                         ^
          856|     if def._type or "" == "merge" then

       … from call site

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/nixos/modules/system/activation/top-level.nix:101:12:

          100|   # Replace runtime dependencies
          101|   system = foldr ({ oldDependency, newDependency }: drv:
             |            ^
          102|       pkgs.replaceDependency { inherit oldDependency newDependency drv; }

       … while calling 'foldr'

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/lists.nix:53:20:

           52|   */
           53|   foldr = op: nul: list:
             |                    ^
           54|     let

       … from call site

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/lists.nix:60:8:

           59|         else op (elemAt list n) (fold' (n + 1));
           60|     in fold' 0;
             |        ^
           61|

       … while calling 'fold''

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/lists.nix:56:15:

           55|       len = length list;
           56|       fold' = n:
             |               ^
           57|         if n == len

       … from call site

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/nixos/modules/system/activation/top-level.nix:98:10:

           97|     then throw "\nFailed assertions:\n${concatStringsSep "\n" (map (x: "- ${x}") failedAssertions)}"
           98|     else showWarnings config.warnings baseSystem;
             |          ^
           99|

       … while calling 'showWarnings'

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/trivial.nix:414:28:

          413|
          414|   showWarnings = warnings: res: lib.foldr (w: x: warn w x) res warnings;
             |                            ^
          415|

       … from call site

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/trivial.nix:414:33:

          413|
          414|   showWarnings = warnings: res: lib.foldr (w: x: warn w x) res warnings;
             |                                 ^
          415|

       … while calling 'foldr'

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/lists.nix:53:20:

           52|   */
           53|   foldr = op: nul: list:
             |                    ^
           54|     let

       … from call site

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/lists.nix:60:8:

           59|         else op (elemAt list n) (fold' (n + 1));
           60|     in fold' 0;
             |        ^
           61|

       … while calling 'fold''

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/lists.nix:56:15:

           55|       len = length list;
           56|       fold' = n:
             |               ^
           57|         if n == len

       … while calling 'g'

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/attrsets.nix:599:19:

          598|           g =
          599|             name: value:
             |                   ^
          600|             if isAttrs value && cond value

       … from call site

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/attrsets.nix:602:20:

          601|               then recurse (path ++ [name]) value
          602|               else f (path ++ [name]) value;
             |                    ^
          603|         in mapAttrs g;

       … while calling anonymous lambda

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/modules.nix:242:72:

          241|           # For definitions that have an associated option
          242|           declaredConfig = mapAttrsRecursiveCond (v: ! isOption v) (_: v: v.value) options;
             |                                                                        ^
          243|

       … while evaluating the option `warnings':

       … while calling anonymous lambda

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/modules.nix:783:28:

          782|         # Process mkMerge and mkIf properties.
          783|         defs' = concatMap (m:
             |                            ^
          784|           map (value: { inherit (m) file; inherit value; }) (builtins.addErrorContext "while evaluating definitions from `${m.file}':" (dischargeProperties m.value))

       … while evaluating definitions from `/nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/nixos/modules/system/boot/systemd.nix':

       … from call site

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/modules.nix:784:137:

          783|         defs' = concatMap (m:
          784|           map (value: { inherit (m) file; inherit value; }) (builtins.addErrorContext "while evaluating definitions from `${m.file}':" (dischargeProperties m.value))
             |                                                                                                                                         ^
          785|         ) defs;

       … while calling 'dischargeProperties'

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/modules.nix:855:25:

          854|   */
          855|   dischargeProperties = def:
             |                         ^
          856|     if def._type or "" == "merge" then

       … while calling anonymous lambda

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/attrsets.nix:543:10:

          542|     attrs:
          543|     map (name: f name attrs.${name}) (attrNames attrs);
             |          ^
          544|

       … from call site

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/attrsets.nix:543:16:

          542|     attrs:
          543|     map (name: f name attrs.${name}) (attrNames attrs);
             |                ^
          544|

       … while calling anonymous lambda

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/nixos/modules/system/boot/systemd.nix:422:16:

          421|       mapAttrsToList
          422|         (name: service:
             |                ^
          423|           let

       … from call site

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/nixos/modules/system/boot/systemd.nix:429:16:

          428|             concatLists [
          429|               (optional (type == "oneshot" && (restart == "always" || restart == "on-success"))
             |                ^
          430|                 "Service '${name}.service' with 'Type=oneshot' cannot have 'Restart=always' or 'Restart=on-success'"

       … while calling 'optional'

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/lists.nix:285:20:

          284|   */
          285|   optional = cond: elem: if cond then [elem] else [];
             |                    ^
          286|

       … while calling 'g'

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/attrsets.nix:599:19:

          598|           g =
          599|             name: value:
             |                   ^
          600|             if isAttrs value && cond value

       … from call site

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/attrsets.nix:602:20:

          601|               then recurse (path ++ [name]) value
          602|               else f (path ++ [name]) value;
             |                    ^
          603|         in mapAttrs g;

       … while calling anonymous lambda

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/modules.nix:242:72:

          241|           # For definitions that have an associated option
          242|           declaredConfig = mapAttrsRecursiveCond (v: ! isOption v) (_: v: v.value) options;
             |                                                                        ^
          243|

       … while evaluating the option `systemd.services.home-manager-user.serviceConfig':

       … from call site

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/modules.nix:805:59:

          804|       if isDefined then
          805|         if all (def: type.check def.value) defsFinal then type.merge loc defsFinal
             |                                                           ^
          806|         else let allInvalid = filter (def: ! type.check def.value) defsFinal;

       … while calling 'merge'

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/types.nix:538:20:

          537|       check = isAttrs;
          538|       merge = loc: defs:
             |                    ^
          539|         mapAttrs (n: v: v.value) (filterAttrs (n: v: v ? value) (zipAttrsWith (name: defs:

       … from call site

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/types.nix:539:35:

          538|       merge = loc: defs:
          539|         mapAttrs (n: v: v.value) (filterAttrs (n: v: v ? value) (zipAttrsWith (name: defs:
             |                                   ^
          540|             (mergeDefinitions (loc ++ [name]) elemType defs).optionalValue

       … while calling 'filterAttrs'

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/attrsets.nix:309:5:

          308|     # The attribute set to filter
          309|     set:
             |     ^
          310|     listToAttrs (concatMap (name: let v = set.${name}; in if pred name v then [(nameValuePair name v)] else []) (attrNames set));

       … while calling anonymous lambda

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/attrsets.nix:310:29:

          309|     set:
          310|     listToAttrs (concatMap (name: let v = set.${name}; in if pred name v then [(nameValuePair name v)] else []) (attrNames set));
             |                             ^
          311|

       … from call site

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/attrsets.nix:310:62:

          309|     set:
          310|     listToAttrs (concatMap (name: let v = set.${name}; in if pred name v then [(nameValuePair name v)] else []) (attrNames set));
             |                                                              ^
          311|

       … while calling anonymous lambda

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/types.nix:539:51:

          538|       merge = loc: defs:
          539|         mapAttrs (n: v: v.value) (filterAttrs (n: v: v ? value) (zipAttrsWith (name: defs:
             |                                                   ^
          540|             (mergeDefinitions (loc ++ [name]) elemType defs).optionalValue

       … while calling anonymous lambda

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/types.nix:539:86:

          538|       merge = loc: defs:
          539|         mapAttrs (n: v: v.value) (filterAttrs (n: v: v ? value) (zipAttrsWith (name: defs:
             |                                                                                      ^
          540|             (mergeDefinitions (loc ++ [name]) elemType defs).optionalValue

       … while calling anonymous lambda

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/modules.nix:783:28:

          782|         # Process mkMerge and mkIf properties.
          783|         defs' = concatMap (m:
             |                            ^
          784|           map (value: { inherit (m) file; inherit value; }) (builtins.addErrorContext "while evaluating definitions from `${m.file}':" (dischargeProperties m.value))

       … while evaluating definitions from `/nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/flake.nix':

       … from call site

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/modules.nix:784:137:

          783|         defs' = concatMap (m:
          784|           map (value: { inherit (m) file; inherit value; }) (builtins.addErrorContext "while evaluating definitions from `${m.file}':" (dischargeProperties m.value))
             |                                                                                                                                         ^
          785|         ) defs;

       … while calling 'dischargeProperties'

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/modules.nix:855:25:

          854|   */
          855|   dischargeProperties = def:
             |                         ^
          856|     if def._type or "" == "merge" then

       … while evaluating derivation 'home-manager-generation'
         whose name attribute is located at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/pkgs/stdenv/generic/make-derivation.nix:300:7

       … while evaluating attribute 'buildCommand' of derivation 'home-manager-generation'

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/pkgs/build-support/trivial-builders/default.nix:87:14:

           86|       enableParallelBuilding = true;
           87|       inherit buildCommand name;
             |              ^
           88|       passAsFile = [ "buildCommand" ]

       … while evaluating derivation 'activation-script'
         whose name attribute is located at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/pkgs/stdenv/generic/make-derivation.nix:300:7

       … while evaluating attribute 'text' of derivation 'activation-script'

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/pkgs/build-support/trivial-builders/default.nix:146:16:

          145|     runCommand name
          146|       { inherit text executable checkPhase allowSubstitutes preferLocalBuild;
             |                ^
          147|         passAsFile = [ "text" ];

       … while calling 'mkCmd'

         at /nix/store/4kkvfkkhr53w8l4kc82j1pml32j3kjrk-source/modules/home-environment.nix:666:17:

          665|       let
          666|         mkCmd = res: ''
             |                 ^
          667|             _iNote "Activating %s" "${res.name}"

       … while calling 'g'

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/attrsets.nix:599:19:

          598|           g =
          599|             name: value:
             |                   ^
          600|             if isAttrs value && cond value

       … from call site

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/attrsets.nix:602:20:

          601|               then recurse (path ++ [name]) value
          602|               else f (path ++ [name]) value;
             |                    ^
          603|         in mapAttrs g;

       … while calling anonymous lambda

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/modules.nix:242:72:

          241|           # For definitions that have an associated option
          242|           declaredConfig = mapAttrsRecursiveCond (v: ! isOption v) (_: v: v.value) options;
             |                                                                        ^
          243|

       … while evaluating the option `home-manager.users.user.home.activation.installPackages.data':

       … while calling anonymous lambda

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/modules.nix:783:28:

          782|         # Process mkMerge and mkIf properties.
          783|         defs' = concatMap (m:
             |                            ^
          784|           map (value: { inherit (m) file; inherit value; }) (builtins.addErrorContext "while evaluating definitions from `${m.file}':" (dischargeProperties m.value))

       … while evaluating definitions from `/nix/store/4kkvfkkhr53w8l4kc82j1pml32j3kjrk-source/modules/home-environment.nix':

       … from call site

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/modules.nix:784:137:

          783|         defs' = concatMap (m:
          784|           map (value: { inherit (m) file; inherit value; }) (builtins.addErrorContext "while evaluating definitions from `${m.file}':" (dischargeProperties m.value))
             |                                                                                                                                         ^
          785|         ) defs;

       … while calling 'dischargeProperties'

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/lib/modules.nix:855:25:

          854|   */
          855|   dischargeProperties = def:
             |                         ^
          856|     if def._type or "" == "merge" then

       … while evaluating derivation 'home-manager-path'
         whose name attribute is located at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/pkgs/stdenv/generic/make-derivation.nix:300:7

       … while evaluating attribute 'passAsFile' of derivation 'home-manager-path'

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/pkgs/build-support/trivial-builders/default.nix:88:7:

           87|       inherit buildCommand name;
           88|       passAsFile = [ "buildCommand" ]
             |       ^
           89|         ++ (derivationArgs.passAsFile or []);

       … while evaluating derivation 'emacs-with-packages-29.0.92'
         whose name attribute is located at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/pkgs/stdenv/generic/make-derivation.nix:300:7

       … while evaluating attribute 'deps' of derivation 'emacs-with-packages-29.0.92'

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/pkgs/build-support/emacs/wrapper.nix:60:5:

           59|     # one path to the load lists
           60|     deps = runCommand "emacs-packages-deps"
             |     ^
           61|       ({

       … while evaluating derivation 'emacs-packages-deps'
         whose name attribute is located at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/pkgs/stdenv/generic/make-derivation.nix:300:7

       … while evaluating attribute 'explicitRequires' of derivation 'emacs-packages-deps'

         at /nix/store/4qn2xsmahx2qsafqz1snl6r1k5bwna44-source/pkgs/build-support/emacs/wrapper.nix:62:16:

           61|       ({
           62|         inherit explicitRequires lndir emacs;
             |                ^
           63|         nativeBuildInputs = lib.optional withNativeCompilation gcc;

       error: attribute 'packages' missing

       at /nix/store/mlmykpfjlpj00ryry21n9pjin32wcx4j-source/flake.nix:21:23:

           20|           mypkgs = {
           21|             mypkg = inputs.mypkg.packages.${system}.default;
             |                    ^
           22|           };

The repositories are too large to post here. However, when I tried minimizing my config it does work well surprisingly. The error message is too confusing for me to find where the actual error is.

I finally fixed this error by deleting the flake.lock. It seems that with flake.lock, even if I updated my package, it won’t use those changes. That’s also why I can’t reproduce the error when I tried minimizing my config.

Thank you all who helped me with this issue!