nix flake templates are not evaluated properly #478

Open
opened 2024-08-20 11:01:24 +00:00 by evysgarden · 4 comments

Describe the bug
derivations in flake templates are not properly evaluated?

Steps To Reproduce
Consider the following code:

{
  outputs = { self, nixpkgs }:
    let
      pkgs = import nixpkgs { system = "x86_64-linux"; };
      templatePackage = pkgs.stdenv.mkDerivation {
        name = "foo-template-package";
        src = ./.;
        installPhase = ''
          mkdir -p $out
          echo yo > $out/foo
        '';
      };
    in
    {
      templates = {
        foo = {
          path = "${templatePackage}";
        };
      };
    };
}

Calling nix flake init -t ".#foo" gives the following error:

error: opening directory '/nix/store/<some-hash>-foo-template-package': No such file or directory

Expected behavior
The derivation should be actually build so that the path exists.

the template output is not system-specific (unlike for example packages). Also something that doesn't really make sense to me as it should be possible to build templates and building may not be system-agnostic.

Something like outputs.templates.${system}.foo.path = "${pkgs.hello}" should be possible i feel like

nix-env --version output
nix-env (Lix, like Nix) 2.90.0

Priorities

Add 👍 to issues you find important.

**Describe the bug** derivations in flake templates are not properly evaluated? **Steps To Reproduce** Consider the following code: ```nix { outputs = { self, nixpkgs }: let pkgs = import nixpkgs { system = "x86_64-linux"; }; templatePackage = pkgs.stdenv.mkDerivation { name = "foo-template-package"; src = ./.; installPhase = '' mkdir -p $out echo yo > $out/foo ''; }; in { templates = { foo = { path = "${templatePackage}"; }; }; }; } ``` Calling `nix flake init -t ".#foo"` gives the following error: ```sh error: opening directory '/nix/store/<some-hash>-foo-template-package': No such file or directory ``` **Expected behavior** The derivation should be actually build so that the path exists. the template output is not system-specific (unlike for example packages). Also something that doesn't really make sense to me as it should be possible to build templates and building may not be system-agnostic. Something like `outputs.templates.${system}.foo.path = "${pkgs.hello}"` should be possible i feel like **`nix-env --version` output** `nix-env (Lix, like Nix) 2.90.0` **Priorities** Add :+1: to [issues you find important](https://github.com/NixOS/nix/issues?q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc).
Author

I've had this discussion on cppnix already and felt like maybe it'd find more tracktion here.
Here's the original issue tho: https://github.com/NixOS/nix/issues/11309

I've had this discussion on cppnix already and felt like maybe it'd find more tracktion here. Here's the original issue tho: https://github.com/NixOS/nix/issues/11309
Owner

wait the paths are not realized??! holy crap flakes are so broken.

i have a bad solution for you: builtins.seq (builtins.length (builtins.listDir drv)) drv, which should force an import-from-derivation build of the thing.

anyway, I'm going to mark this as help wanted because i think my current temperature read on the lix team is that fixing bugs in flakes is the least enjoyable activity imaginable so this is most likely to get done if someone else sends a patch.

wait the paths are not realized??! holy crap flakes are so broken. i have a bad solution for you: `builtins.seq (builtins.length (builtins.listDir drv)) drv`, which should force an import-from-derivation build of the thing. anyway, I'm going to mark this as help wanted because i think my current temperature read on the lix team is that fixing bugs in flakes is the least enjoyable activity imaginable so this is most likely to get done if someone else sends a patch.
jade added the
bug
E/help wanted
Area/flakes
labels 2024-08-20 16:29:27 +00:00
Owner

i will say, i think this is probably a bad idea to fix without changing the schema of flakes because as the cppnix maintainers pointed out, there are no system specific templates. so just fixing this by itself is probably going to inadvertently encourage people to write x86_64-linux only templates.

the more realistic thing to do today is some kind of construction based on nix run, imo.

i will say, i think this is probably a bad idea to fix without changing the schema of flakes because as the cppnix maintainers pointed out, there are no system specific templates. so just fixing this by itself is probably going to inadvertently encourage people to write x86_64-linux only templates. the more realistic thing to do *today* is some kind of construction based on nix run, imo.
Author

That's what I will prbly do.

Altho I want to point out that how people are gonna write their templates is not that important imo. I have seen x86_64-linux only packages where the maintainer just didn't use mkFlake. The same is gonna happen with templates, no doubt.

My plan was to use a tool like mkFlake in my templates so that wouldn't make the code significantly more complicated either.

That's what I will prbly do. Altho I want to point out that how people are gonna write their templates is not that important imo. I have seen x86_64-linux only packages where the maintainer just didn't use `mkFlake`. The same is gonna happen with templates, no doubt. My plan was to use a tool like `mkFlake` in my templates so that wouldn't make the code significantly more complicated either.
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: lix-project/lix#478
No description provided.