with-inputs icon indicating copy to clipboard operation
with-inputs copied to clipboard

Nix evaluation error when an input uses flake-parts

Open clakoclast opened this issue 2 months ago • 3 comments

I am happily using with-inputs for a non-Flake project, with npins pulling in some flakes and non-flakes, but I tried adding a flake repo that uses flake-parts and got a confusing error.

This Bash script is a minimal example that re-creates the same error:

#! /usr/bin/env bash

mkdir /tmp/test

# Set up a minimal flake-parts flake using flake-parts' init template, which exports `packages.default` 
cd /tmp/test
mkdir flake-parts-hello
cd flake-parts-hello
nix --extra-experimental-features flakes flake init -t github:hercules-ci/flake-parts
git init --initial-branch main
git add . && git commit -m "init: flake-parts template"

# Create a minimal not-flake nix project using with-inputs and npins that depends on the above flake-parts-hello
cd /tmp/test
mkdir with-inputs-hello
cd with-inputs-hello
npins init
npins add github denful with-inputs --branch main
npins add git file:/tmp/test/flake-parts-hello --branch 'main'

cat << EOF > default.nix
let
   sources = import ./npins;
   with-inputs = import sources.with-inputs sources {};

   outputs = inputs: {
     packages.default = inputs.flake-parts-hello.packages.default;
   };
in 
with-inputs outputs
EOF


# Try running `hello` via with-inputs-hello
nix run --file default.nix 'packages.default'

The last line resulted in this error:

error:
       … while evaluating an expression to select 'packages' on it
         at /tmp/test/with-inputs-hello/default.nix:8:50:
            7|    outputs = inputs: {
            8|      packages.default = inputs.flake-parts-hello.packages.hello;
             |                                                  ^
            9|    };

       … while evaluating self
         at /nix/store/k6vflcbgj5q5cnnfqfi4cg3pnxv5g922-source/with-inputs.nix:170:5:
          169|     in
          170|     self;
             |     ^
          171|

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

       error: attribute 'lib' missing
       at /nix/store/5rcpjz7wqmcw6jl7vrz1630cjiciscdc-flake-parts-hello-6acf8c7/flake.nix:10:17:
            9|   outputs = inputs@{ flake-parts, ... }:
           10|     flake-parts.lib.mkFlake { inherit inputs; } {
             |                 ^
           11|       imports = [
	

clakoclast avatar Sep 11 '26 01:09 clakoclast

I think I found the issue... as advertised, with-inputs does not fetch anything by itself, and that means transitive dependencies need to be fetched some other way.

The error messages that show up are like the one in my first post and are not especially helpful. I just inspected flake-parts' flake.nix to see its dependencies, which is just nixpkgs.lib. Except flake-parts internally names nixpkgs.lib to nixpkgs-lib and downstream projects need to do that too...

Then I see that nixpkg.lib has a weird entry point that was causing more confusing evaluation errors:

       error: function 'outputs' called with unexpected arguments '__functor', 'with-inputs', 'flake-parts-hello', 'flake-parts' and 'nixpkgs'
       at /nix/store/r241yqbqjw5vf918c6m03fngsi89v28y-source/lib/flake.nix:5:5:
            4|   outputs =
            5|     { self }:
             |     ^
            6|     let

At this point, after some experimentation, this follows line from Vic's infra repo finally solved the error (although I have no idea why defining a nixpkgs follows for nixpkgs.lib fixed the above error...)

cd /tmp/test/with-inputs-hello

# Add flake-parts
npins add github hercules-ci flake-parts --branch main

# flake-parts depends on nixpkgs.lib but it has to be named `nixpkgs-lib`
npins add github nix-community nixpkgs.lib --branch master --name nixpkgs-lib

# nixpkgs.lib created the confusing evaluation errors at this point...
# Adding the `follows` solved the errors
cat < EOF > default.nix
let
   sources = import ./npins;
   with-inputs = import sources.with-inputs sources (inputs: { nixpkgs-lib.follows = "nixpkgs"; });

   outputs = inputs: {
     packages.default = inputs.flake-parts-hello.packages.x86_64-linux.default;
   };
in 
with-inputs outputs
EOF

# Now it works
nix run --file default.nix 'packages.default' 

clakoclast avatar Sep 11 '26 11:09 clakoclast

@clakoclast If you don't want to add flake-parts to your npins you can also load flake-parts-hello using its own flake.lock:

with-inputs = import sources.with-inputs sources (
  inputs: {
    flake-parts-hello = s: inputs.with-inputs.from.flake 
      s { root.outPath = s; nixpkgs-lib = inputs.nixpkgs; } (i: i.root);
  });

(the nixpkgs-lib = inputs.nixpkgs; part is only necessary until https://github.com/denful/with-inputs/pull/22 get merged)

jbgi avatar Sep 13 '26 15:09 jbgi

Thanks for the hint about using with-inputs.from.flake, I did not see anything about it in the readme. It is nice to avoid having to specify every transitive dependency, though the API of from.flake is somewhat confusing to me.

clakoclast avatar Sep 14 '26 07:09 clakoclast