Skip to content

Allow plain Nix expressions as flake inputs #5663

Open

Description

@thufschmitt

(This is mostly a reformulation and extension of #3843 (comment). I’ve been meaning to post this as its own issue for a long time as it’s a potential solution for a bunch of different issues)

As a possible way out of the combinatorial explosion of flake outputs required by the impossibility to pass arguments to flakes (the system issue, dirty things like #4996 required to accomodate everyone’s need, etc..), we could add a new type of flake input nixExpr which would inline a Nix expression.

Motivations

Fix #3843

I’m not sure which one would be best, but this would enable several solutions to #3843:

  • Flakes could effectively take the current system as input by having inputs.system = { type = "nixExpr"; value = "x86_64-linux"; } which could be changed with --override-input system nixExpr:x86_64-darwin. Probably needs a better UX, but the gist of the mechanism would be here.
  • Less invasive (and more eval-cache-friendly), we could keep the packages.{system}.foo structure, but have inputs.allowedSystems = { type = "nixExpr"; value = [ "x86_64-linux", "x86_64-darwin" ]; }, so that if someone wants to build the flake for another system, it’s possible to override this list

More generally, allow for parametrized matrix builds

  • I’m not really familiar with all the details of cross-compilation, but the system approach is probably pretty limited if one wants to express all the possible cross-compilation strategies between a few different system types
  • Restore the ability to build with clang on linux #4129 also wants to add a new dimension to the list of possible builds of the Nix flake. Currently the values of this new dimension have to be hardcoded, but with this approach, we could just have inputs.stdenv.value = "stdenv" (and define the actually used stdenv as stdenv = nixpkgs.${inputs.stdenv}), so that ppl wanting to use clang can just do --override-input stdenv "nixExpr://clangStdenv" (or whatever)

Design proposal

(This is only a first draft, could certainly be refined).

  • Flakes would have a new input type nixExpr.
  • This input type would take as only argument value, which is a json-style Nix value. So not an arbitrary expression, but something arbitrarily (though finitely, rec shouldn’t be allowed) nested
  • Like the other input types, it would also have a url-like form: value:[expr]
  • Like any other input, it could be overriden from the cli using --override-input. Which would be similar to passing arguments to the flake

Design considerations

  • Having to use --override-input foo value:xyz to pass an argument isn’t the nicest syntax. Maybe we could have a --arg foo xyz flag that would be syntactic sugar over this
  • Overriding string inputs wouldn’t be very nice. If I want to override an input such as inputs.system.value = "x86_64-linux", I need to do --override-input system value:\"x86_64-darwin\" (with the escaped quotations, because it has to be a Nix string).
    • Maybe we could also add a --arg-str flag like the old CLI had for this (arguably common) use-case
    • Alternatively, we could have --arg do some magic that would make --arg foo xyz equivalent to --arg foo \"xyz\" if xyz is alphanumeric++ − that would work because it wouldn’t be valid otherwise as arg can’t contain variables or keywords.

Purity concerns

The main reason for not allowing arguments to flakes is that they’re supposed to be pure self-contained entities.
This doesn’t break this property as these “arguments” are just a special type of flake inputs − meaning that for example they are reflected in the lockfile and taken into account when computing the hash of the flake. Besides, this is already possible to emulate in a dirty − and arguably more impure − way using relative paths as inputs and overwriting them on the fly)

/cc @tomberek since you suggested the same idea on matrix
/cc @Mic92 @domenkozar

Activity

  1. nixos-discourse commented on Nov 27, 2021

    @nixos-discourse

    This issue has been mentioned on NixOS Discourse. There might be relevant details there:

    https://discourse.nixos.org/t/passing-options-to-flakes/7579/7

  2. Kha commented on Nov 27, 2021

    @Kha
    Contributor

    Another benefit over the old --arg is that by consolidating all arguments at the flake.nix toplevel, perhaps with an optional docstring, it should be easy to implement cmdline completion for --arg. One could even go one step further with this and allow --debug true as a convenient shorthand for --arg debug true (where true definitely shouldn't be stringified btw /cc @regnat). Or even --debug and --no-debug, but that would be cleaner if arguments also had an associated type.

  3. nixos-discourse commented on Nov 29, 2021

    @nixos-discourse

    This issue has been mentioned on NixOS Discourse. There might be relevant details there:

    https://discourse.nixos.org/t/passing-options-to-flakes/7579/8

  4. edolstra commented on Nov 29, 2021

    @edolstra
    Member

    I'm not convinced this is a good idea, and it could lead to a lot of anti-patterns.

    Conceptually, changing flake.lock is not different from changing flake.nix - they both dirty the flake. So using it to pass parameters like system is a non-starter: passing --arg system bla dirties the flake, so I either have to commit flake.lock every time I pass a different system, or accept that my flake doesn't have a Git revision anymore (so no more provenance).

    In addition, such inputs are global and can't be overriden outside of the flake input mechanism. So for instance, if I need a flake output for two different values of system, I would have to clone the corresponding flake input, e.g.

    {
      inputs.nixpkgs_x86_64-linux = { url = github:NixOS/nixpkgs; args.system = "x86_64-linux"; };
      inputs.nixpkgs_x86_64-darwin = { url = github:NixOS/nixpkgs; args.system = "x86_64-darwin"; };
    
      outputs = { self, nixpkgs_x86_64-linux, nixpkgs_x86_64-darwin, ... }: { ... };
    }
    

    P.S. the approach in dc4a280 was to implement --arg by generating a temporary flake that depends on the original flake and passes new Nix module options. But this requires some type of generic override mechanism (like Nix modules) which we currently don't have.

  5. Mic92 commented on Nov 29, 2021

    @Mic92
    Member

    Does dc4a280 require flakes to be in toml format for overrides to work?

  6. thufschmitt commented on Nov 29, 2021

    @thufschmitt
    MemberAuthor

    Conceptually, changing flake.lock is not different from changing flake.nix - they both dirty the flake

    That’s right for toplevel flakes (though I’m not sure it’s the best possible choice), and that indeed makes passing system as an argument that way not a good option (I new I shouldn’t have included this example 😒 ). But that doesn’t invalidate the idea in general. In particular just being able to have an extensible lists of supportedSystems (or however it would be called) so that it can be overridden for dependent flakes would be incredibly useful

    In addition, such inputs are global and can't be overriden outside of the flake input mechanism. So for instance, if I need a flake output for two different values of system, I would have to clone the corresponding flake input

    That’s also right, but it doesn’t seem like a fundamental issue to me. At least not if we don’t take system as example.
    Otoh, a nice thing with this is that we get the possibility to pass arguments to the dependent flakes. Like

    {
        inputs.debugMode.value = false;
        inputs.foo = {
            url = "...";
            inputs.debugMode.follows = "debugMode";
        };
    }

    (granted it’s not utterly pretty though)

  7. tomberek commented on Jan 17, 2022

    @tomberek
    Contributor

    I'm not convinced this is a good idea, and it could lead to a lot of anti-patterns.

    For the record: some anti-patterns that currently work. Warning: ugly hacks.

    My thought is that you can currently declare a file with a Nix expression as an input and import it. It is not a big step from that to allowing a nix expression directly. The default can be declared in the flake.nix and tracked and locked in the same way that a file would be. Overriding on the CLI is like overriding an input, which disables writing the lock file anyway. The only concern is that doing this may encourage more usage of the underlying mechanism of override-input and reduce overall user experience.

  8. moved this to To discuss in Nix UXon Feb 10, 2022
  9. tomberek commented on Feb 14, 2022

    @tomberek
    Contributor

    PoC that only allows string values: tomberek@a41ee9e

    {
      description = "A string as an input argument";
      inputs.thing.value = "mething233";
    
      outputs = { self, nixpkgs, thing }: with nixpkgs.legacyPackages.x86_64-linux; {
        defaultPackage.x86_64-linux = runCommand "something" {} ''
          echo xxx
          echo value is: ${builtins.readFile thing}
          echo xxx
          '';
      };
    }
    

    spaces are not allowed in urls, so can't be used here, will need to modify fetchers.cc to understand the modification of a value input.

    nix build -L --override-input thing 'value:anotherValue'
    
  10. kamadorueda commented on Mar 10, 2022

    @kamadorueda
    Member

    Summary of the discussion as of 2022-03-10

    #5663

    • Motivation:

      • Having a generic mechanism for passing arguments to the Flake.
    • Use cases:

      • Reducing boilerplate when doing matrix-type configuration of the same package
      • Reducing boilerplate of boolean flags configuration of the same package
      • Reducing boilerplate when trying to use different python versions
      • And more generically, using less code for expressing configurability
    • Criticism of current design:

      • Requires boilerplate to enumerate all possible configurations
    • Pros of current design:

      • It's easily discoverable
  11. jakubgs commented on Apr 20, 2022

    @jakubgs

    Lack of arguments/options for flakes makes it impossible to use Flakes for builds of our mobile application.

    We have various arguments that we pass to the builds like build number, extra Gradle arguments, or architectures. The issue with architectures is that an Android APK can include multiple architectures, so using the system argument would have include a product of every architecture combination. For example: armeabi-v7a;arm64-v8a, armeabi-v7a;x86, arm64-v8a;x86, and so on. Which isn't the prettiest solution.

    Combined with other arguments - like build number - makes using separate build targets not feasible, since it's just a number.

  12. moved this from To discuss to Backlog in Nix UXon May 5, 2022
  13. LHurttila commented on Aug 28, 2022

    @LHurttila

    PoC that only allows string values: tomberek@a41ee9e

    {
      description = "A string as an input argument";
      inputs.thing.value = "mething233";
    
      outputs = { self, nixpkgs, thing }: with nixpkgs.legacyPackages.x86_64-linux; {
        defaultPackage.x86_64-linux = runCommand "something" {} ''
          echo xxx
          echo value is: ${builtins.readFile thing}
          echo xxx
          '';
      };
    }
    

    spaces are not allowed in urls, so can't be used here, will need to modify fetchers.cc to understand the modification of a value input.

    nix build -L --override-input thing 'value:anotherValue'
    

    Looks great but can't figure out how to use that PoC. Seems like it adds the funtionality to nix directly so can I somehow patch it in the same nixos flake that would use it in its inputs? Any example would be really welcome.

  14. jhol commented on Oct 12, 2022

    @jhol

    @tomberek this looks very promising. Did you have any plans to submit the PoC?

  15. 16 remaining items

  16. roberth commented on Mar 3, 2024

    @roberth
    Member

    With an over-reliance on input overriding like this, we'll have no place for actual configurations, in the sense that a configuration is an arrangement where everything is defined and no parameters are left. The outputs attribute is currently our best place for such things.
    An input can affect anything in the flake, which is messy. It will be unclear which parameters can or should be provided, to affect a given output attribute.
    After all this time, haven't we learned that global state is not so great? One could be mistaken to think that it's a core value of Nix...
    Of course we're going to need to override and configure things, but it should not happen at the root of outputs.
    How about we create a new concept "overridable", which exists inside outputs (so that multiple can exist side by side), each with their own set of inputs, and each returning only the attributes that are actually affected by the setting. And let's just call them functions. Functions with optional defaults. If it has defaults for everything, great, it can be promoted to the top level. (Maybe automatically, maybe not)

  17. Ma27 commented on Mar 3, 2024

    @Ma27
    Member

    @roberth just to avoid we're talking past each other: is this a criticism on the original suggestion or on the alternative that I've brought up? Because "It will be unclear which parameters can or should be provided" doesn't sound like an issue about something that explicitly defines options and defaults for that (i.e. configurable flakes).

    each with their own set of inputs, and each returning only the attributes that are actually affected by the setting

    But you'd still need to be able to pass arbitrary Nix values to these overridables, correct? Or how else would you pass e.g. allowUnfree to an overridable which instantiates nixpkgs?

  18. roberth commented on Mar 5, 2024

    @roberth
    Member

    Both suffer from the same issue, being flake-wide. Documentation does help, but "global" settings are not a great design, and a separation between inputs and configuration is a bit artificial or unnecessary - why not have structured docs for the inputs?

    But you'd still need to be able to pass arbitrary Nix values to these overridables, correct?

    Yes, and these would be mostly normal functions (depending on caching, possibly), so that would be supported.

  19. Atry commented on Mar 20, 2024

    @Atry
    Contributor

    PoC that only allows string values: tomberek@a41ee9e

    {
      description = "A string as an input argument";
      inputs.thing.value = "mething233";
    
      outputs = { self, nixpkgs, thing }: with nixpkgs.legacyPackages.x86_64-linux; {
        defaultPackage.x86_64-linux = runCommand "something" {} ''
          echo xxx
          echo value is: ${builtins.readFile thing}
          echo xxx
          '';
      };
    }
    

    spaces are not allowed in urls, so can't be used here, will need to modify fetchers.cc to understand the modification of a value input.

    nix build -L --override-input thing 'value:anotherValue'
    

    We don't need the patch. We can do this right now using file+file protocol:

    {
      description = "A string as an input argument";
      inputs.thing.url = "file+file:///dev/null";
      inputs.thing.flake = false;
    
      outputs = { self, nixpkgs, thing }: with nixpkgs.legacyPackages.x86_64-linux; {
        packages.x86_64-linux.default = runCommand "something" {} ''
          echo xxx
          echo value is: ${builtins.readFile thing.outPath}
          echo xxx
          '';
      };
    }
    THING=anotherValue
    nix build --override-input thing file+file://<(printf %s "$THING")
  20. Ma27 commented on Apr 13, 2024

    @Ma27
    Member

    How about we create a new concept "overridable", which exists inside outputs (so that multiple can exist side by side), each with their own set of inputs, and each returning only the attributes that are actually affected by the setting. And let's just call them functions. Functions with optional defaults. If it has defaults for everything, great, it can be promoted to the top level

    @roberth I'm sorry, but I still have a hard time parsing this.

    What does input mean here? Are we actually talking about flake inputs? And what does setting mean in the context?

    I think I'll need an example here.

  21. roberth commented on Apr 14, 2024

    @roberth
    Member

    I was a little bit facetious because I find Flake's reinvention of the wheel frustrating. I'm sorry that that made my message unclear.

    "Input" applied to both flake inputs and function arguments, as I was attempting to reconcile those two concepts.

    I don't know if I have a particularly actionable suggestion or great example, because whenever I try to come up with something, I have an urge to first tear a whole bunch of things down. For instance, we should really have something that reconciles

    • inputs
    • "flake configuration"
    • the system parameter
    • bundler argument builders (in the sense of flakes; not in the sense of derivations; not in the sense of Nixpkgs; not in the sense of remote builds)

    But this is basically impossible without also replacing them.

    Finally if we want "discoverability" in "configuration", we also need to respect the interface segregation principle, because, e.g. inputs.nixpkgs must not apply to overlays (or if it does, that is very important to convey to users!), so even our familiar context of outputs = { nixpkgs, ... }: ... can't be kept in a good design of configurability.

    Again, I'm sorry that I don't have good answers, but hopefully we can begin to clarify what's even the question.

  22. nikolaiser commented on May 6, 2024

    @nikolaiser

    Based on the suggestion to use the file+file protocol by @Atry I created a small cli tool that allows to pass arguments to flakes with the syntax --arg key1=value1 --arg key2=value2

    https://github.com/nikolaiser/purga

  23. Lindenk commented on Jun 21, 2024

    @Lindenk

    I also wanted to mention that passing inputs to a build is required for nix to be a viable build system for me as well. Even something as simple as setting the version number from CI needs the above hacky workaround at the moment

  24. daveman1010221 commented on Nov 14, 2024

    @daveman1010221

    I also wanted to mention that passing inputs to a build is required for nix to be a viable build system for me as well. Even something as simple as setting the version number from CI needs the above hacky workaround at the moment

    This is exactly how I found this thread, trying to build a container from a flake, within a CI/CD server's configuration environment. We are currently using "impure" as our work-around here. This is a hard limitation on the usefulness of flakes, honestly.

  25. Naxdy commented on Feb 24, 2025

    @Naxdy

    Just wanted to add my own personal workaround for those needing this feature. I'm simply using flake-compat in my projects, and it does the job. Here is a minimal example flake:

    {
      description = "A very basic flake";
    
      inputs = {
        nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
    
        flake-compat.url = "https://flakehub.com/f/edolstra/flake-compat/1.tar.gz";
    
        flake-utils.url = "github:numtide/flake-utils";
      };
    
      outputs =
        {
          self,
          nixpkgs,
          flake-utils,
          ...
        }:
        flake-utils.lib.eachDefaultSystem (
          system:
          let
            pkgs = import nixpkgs { inherit system; };
          in
          {
            lib.myfunction =
              {
                arginput ? "no argument provided",
              }:
              builtins.throw arginput;
          }
        );
    }
    $ nix build --file ./default.nix lib.x86_64-linux.myfunction                                 
    error:
           … while calling the 'throw' builtin
             at /nix/store/4b28hnzb6ip1wwk1xk23g1spsjhpp7hb-thing/flake.nix:29:11:
               28|           }:
               29|           builtins.throw arginput;
                 |           ^
               30|       }
    
           error: no argument provided
    
    $ nix build --file ./default.nix lib.x86_64-linux.myfunction --argstr arginput "custom input"
    error:
           … while calling the 'throw' builtin
             at /nix/store/4b28hnzb6ip1wwk1xk23g1spsjhpp7hb-thing/flake.nix:29:11:
               28|           }:
               29|           builtins.throw arginput;
                 |           ^
               30|       }
    
           error: custom input

    No need for --impure or file system inputs.

  26. ruro commented on Mar 12, 2025

    @ruro

    @edolstra I just watched your "Configurable Flakes" talk from PlanetNix, and I am quite excited about it as this seems like exactly the kind of improvement of flakes that I've been hoping for.

    I have two questions:

    1. You mentioned that configurablePackages isn't hardcoded in nix itself, but instead specified via flake schemas and showed the code for configurablePackagesSchema. However, I don't see this schema mentioned anywhere in github:edolstra/configurable-flake. Is adding custom schemas just currently unsupported or is it specified somewhere and I am just blind?

    2. I've noticed that at the end of the presentation, the "Try it out" section mentions DeterminateSystems/nix-src rather than NixOS/nix:

      nix shell github:DeterminateSystems/nix-src/configurable-flakes
      nix describe github:edolstra/configurable-flake
      nix run github:edolstra/configurable-flake --enable big
      ...

      This makes me wonder whether you are planning to upstream this Configurable Flakes feature to "proper" nix after it is ready, or if it is somehow specific to Determinate Nix?

    Either way, huge thanks for working on this feature, it looks really cool! 🙏

  27. svenssonaxel commented on Jun 15, 2025

    @svenssonaxel

    Conceptually, changing flake.lock is not different from changing flake.nix - they both dirty the flake. So using it to pass parameters like system is a non-starter: passing --arg system bla dirties the flake, so I either have to commit flake.lock every time I pass a different system, or accept that my flake doesn't have a Git revision anymore (so no more provenance).

    @edolstra It seems to me we can already build different things from the same git revision by overriding inputs and provide an attribute path, all without changing flake.lock. Both are conceptually very similar to an argument already and I don't see how supporting --arg would add any purity concerns. The key that uniquely identifies a reproducible build output would merely expand from (git_commit, overridden_inputs, attribute_path, output_name) to that plus arguments. What am I missing here?

  28. nixos-discourse commented on Oct 26, 2025

    @nixos-discourse
  29. yaitskov commented on Feb 14, 2026

    @yaitskov
  30. nixos-discourse commented on Jul 11, 2026

    @nixos-discourse
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

    Projects

    • Status
      Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions