Skip to content

error: '/nix/store/.../flake.nix' must be an attribute set  #3966

Open

Description

@nmattia

Describe the bug

This flake.nix evaluates to an attribute set:

let
  foo = "bar";
in
{
  outputs = { self, nixpkgs }:
    {
      packages.x86_64-linux.devShell = nixpkgs.outputs.legacyPackages.x86_64-linux.hello;
    };
}

however nix develop (or nix build .#devShell) results in the following error:

$ nix develop
error: --- Error ------------------------------------------------------------------------------------ nix
file '/nix/store/wvlsr8s3nysj0pmgdl7ndggpxc8za87f-source/flake.nix' must be an attribute set
(use '--show-trace' to show detailed location information)

I can make nix happy by moving the let inside the outputs definition:

{
  outputs = { self, nixpkgs }:
    let
      foo = "bar";
    in
      {
        packages.x86_64-linux.devShell = nixpkgs.outputs.legacyPackages.x86_64-linux.hello;
      };
}

Expected behavior

I'm expecting the flake commands to allow top-level let bindings; but maybe that's on purpose to ensure the flake.nix stays as simple as possible?

nix-env --version output

nix-env (Nix) 2.4pre20200721_ff314f1

Activity

  1. zimbatm commented on Aug 28, 2020

    @zimbatm
    Member

    That's a strange limitation of how flakes are being evaluated. All of the data structures must be pretty much JSON-like data. The only place where more complex nix code is allowed is within the outputs function.

  2. edolstra commented on Aug 28, 2020

    @edolstra
    Member

    but maybe that's on purpose to ensure the flake.nix stays as simple as possible?

    That's right. It ensures that a command like nix flake info doesn't have to evaluate an arbitrarily complex (and possibly non-terminating) Nix expression.

  3. nmattia commented on Aug 28, 2020

    @nmattia
    ContributorAuthor

    Ok I realize it's actually terribly picky, even thunks are forbidden (i.e. in an input's { url = "foo" + "bar"; }):

    /tmp/tmp.7jJ3XRKbVP$ nix develop
    error: --- Error ------------------------------------------------------------------------------------- nix
    expected a string but got a thunk at /nix/store/4yiaxz02925lr1ssyir8nc7cs8dp7bf0-source/flake.nix:2:3
    (use '--show-trace' to show detailed location information)
    
  4. jorsn commented on Sep 19, 2020

    @jorsn
    Member

    Why then use nix syntax if it is not nix? I'd say, either use a clearly defined subset of nix code, at best with another name and file ending (tnix – total nix) or the flake file shall be in a total language like plain json and the outputs attr refers to nix expressions, or dhall (but a comparatively complex new language).

    If it looks like nix but isn't, to me it is confusing unexpected (”strange“ – zimbatm) behaviour.

    Edit: Why not split the flake.nix into flake.json containing metadata and inputs and flake.nix/outputs.nix containing only the outputs function in real Nix lang?

  5. roberth commented on Oct 10, 2020

    @roberth
    Member

    I like the idea of specifying inputs and metadata in json. It's easier to work with for Nix itself and for external tools. With niv I often use niv update some-input -b feature-branch to test things out. nix flake doesn't have such a command and it'd be relatively hard to implement given the current flake format, because we don't have a way to write back modified asts without touching unmodified whitespace and comments.

    I always used to think of the Nix language as a predictable element in an otherwise messy domain. Just a lazy evaluator that implicitly writes derivations. I don't think flakes need to change that.

  6. edolstra commented on Oct 11, 2020

    @edolstra
    Member

    I like the idea of specifying inputs and metadata in json.

    The configs branch adds support for TOML as an alternative to flake.nix (1dc3f53). This allows the flake to be modified from the command line (e.g. 3632593).

    nix flake doesn't have such a command

    Well there is the --override-input flag. Since it only overwrites the lock file it doesn't have to deal with rewriting Nix expressions.

  7. added
    error-messages
    and removed on Feb 9, 2021
  8. added this to the nix-2.4 milestone on Feb 9, 2021
  9. malte-v commented on Mar 7, 2021

    @malte-v

    Ugh. I just spent 8 hours writing a nix expression that dynamically generates flake inputs from an external registry and now I find out that it won't work. Is this limitation really such a great idea? If it's just there to improve the UX of nix flake info I would consider dropping it and allowing arbitrary Nix code in flakes.

  10. gytis-ivaskevicius commented on Jun 6, 2021

    @gytis-ivaskevicius
    Contributor

    Would it be difficult to disallow most builtins instead? 🤔 This would more or less solve the problem with people writing complex expressions and would allow for some flexibility which would be neat.

  11. ggPeti commented on Jun 7, 2021

    @ggPeti
    Member

    Why are we trying to protect users from their own non-terminating nix expressions? On the other hand, why only in commands like nix flake info?

  12. jorsn commented on Jun 7, 2021

    @jorsn
    Member
  13. 50 remaining items

  14. modified the milestones: nix-2.19, nix-2.20 on Nov 20, 2023
  15. chekoopa commented on Jan 7, 2024

    @chekoopa

    We develop a product which includes a server machine based on NixOS with some software modules (developed by us). As for servers, it also implies remote deployment and support.

    So we somehow have survived with plain channels (by pinning them using a few forbidden techniques), but now I'm migrating all of this to flakes (because it refuses to build now).

    The prime issue is dependency tracking. Every our software module is a separate folder in the monorepo, though they interconnect as dependencies (e.g. libraries). The first plan was just importing nixpkgs commit hash from a certain file. But as that's a very unsafe approach, now I'm brewing a single channel dispatcher to somehow provide a unified nixpkgs revision to all the modules in need.

    Thus, yes, tooling is the key, but some unsafe-ty (at least possible to enable implicitly) could help.

  16. jorsn commented on Jan 8, 2024

    @jorsn
    Member

    Here is some tooling proposed in the other issue: #4945 (comment).
    The idea is, you have a flake.template.nix written in ordinary Nix, which is used to generate the actual flake.nix.
    With this, you can write your flake in normal Nix. When it is really worth it (!), then also the small overhead is justified.

    The generation of flake.nix could even be wrapped as a flake app. One could make a flake template (that can be used by nix flake init – not the flake.template.nix) that provides a seed flake.nix only containing the generation app.

  17. jorsn commented on Jan 12, 2024

    @jorsn
    Member

    The generation of flake.nix could even be wrapped as a flake app. One could make a flake template (that can be used by nix flake init – not the flake.template.nix) that provides a seed flake.nix only containing the generation app.

    Implemented: https://github.com/jorsn/flakegen.

  18. modified the milestones: nix-2.20, nix-2.21 on Mar 4, 2024
  19. modified the milestones: nix-2.21, nix-2.22 on Mar 11, 2024
  20. modified the milestones: nix-2.22, nix-2.23 on Apr 23, 2024
  21. removed this from the nix-2.23 milestone on Jun 12, 2024
  22. IogaMaster commented on Jan 4, 2025

    @IogaMaster

    Does nix flake info get the data from flake.nix or flake.lock?
    Because I would think if you need pure data for that function, storing it in the precomputed lockfile would work fine?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    error-messagesflakes

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions