Repository navigation
Description
(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}.foostructure, but haveinputs.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
systemapproach 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 asstdenv = 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,recshouldn’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:xyzto pass an argument isn’t the nicest syntax. Maybe we could have a--arg foo xyzflag 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-strflag like the old CLI had for this (arguably common) use-case - Alternatively, we could have
--argdo some magic that would make--arg foo xyzequivalent to--arg foo \"xyz\"ifxyzis alphanumeric++ − that would work because it wouldn’t be valid otherwise asargcan’t contain variables or keywords.
- Maybe we could also add a
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
nixos-discourse commentedon Nov 27, 2021
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
Kha commentedon Nov 27, 2021
Another benefit over the old
--argis that by consolidating all arguments at theflake.nixtoplevel, 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 trueas a convenient shorthand for--arg debug true(wheretruedefinitely shouldn't be stringified btw /cc @regnat). Or even--debugand--no-debug, but that would be cleaner if arguments also had an associated type.nixos-discourse commentedon Nov 29, 2021
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
edolstra commentedon Nov 29, 2021
I'm not convinced this is a good idea, and it could lead to a lot of anti-patterns.
Conceptually, changing
flake.lockis not different from changingflake.nix- they both dirty the flake. So using it to pass parameters likesystemis a non-starter: passing--arg system bladirties the flake, so I either have to commitflake.lockevery time I pass a differentsystem, 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.P.S. the approach in dc4a280 was to implement
--argby 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.Mic92 commentedon Nov 29, 2021
Does dc4a280 require flakes to be in toml format for overrides to work?
thufschmitt commentedon Nov 29, 2021
That’s right for toplevel flakes (though I’m not sure it’s the best possible choice), and that indeed makes passing
systemas 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 ofsupportedSystems(or however it would be called) so that it can be overridden for dependent flakes would be incredibly usefulThat’s also right, but it doesn’t seem like a fundamental issue to me. At least not if we don’t take
systemas example.Otoh, a nice thing with this is that we get the possibility to pass arguments to the dependent flakes. Like
(granted it’s not utterly pretty though)
tomberek commentedon Jan 17, 2022
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.
tomberek commentedon Feb 14, 2022
PoC that only allows string values: tomberek@a41ee9e
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.
kamadorueda commentedon Mar 10, 2022
Summary of the discussion as of 2022-03-10
Motivation:
Use cases:
Criticism of current design:
Pros of current design:
jakubgs commentedon Apr 20, 2022
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
systemargument 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.
LHurttila commentedon Aug 28, 2022
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.
jhol commentedon Oct 12, 2022
@tomberek this looks very promising. Did you have any plans to submit the PoC?
16 remaining items
roberth commentedon Mar 3, 2024
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
outputsattribute 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)
Ma27 commentedon Mar 3, 2024
@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).
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.
allowUnfreeto an overridable which instantiates nixpkgs?roberth commentedon Mar 5, 2024
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?
Yes, and these would be mostly normal functions (depending on caching, possibly), so that would be supported.
Atry commentedon Mar 20, 2024
We don't need the patch. We can do this right now using
file+fileprotocol:THING=anotherValue nix build --override-input thing file+file://<(printf %s "$THING")Ma27 commentedon Apr 13, 2024
@roberth I'm sorry, but I still have a hard time parsing this.
What does
inputmean here? Are we actually talking about flake inputs? And what does setting mean in the context?I think I'll need an example here.
roberth commentedon Apr 14, 2024
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
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.nixpkgsmust not apply tooverlays(or if it does, that is very important to convey to users!), so even our familiar context ofoutputs = { 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.
nikolaiser commentedon May 6, 2024
Based on the suggestion to use the
file+fileprotocol by @Atry I created a small cli tool that allows to pass arguments to flakes with the syntax--arg key1=value1 --arg key2=value2https://github.com/nikolaiser/purga
Lindenk commentedon Jun 21, 2024
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
daveman1010221 commentedon Nov 14, 2024
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.
Naxdy commentedon Feb 24, 2025
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:
$ 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 inputNo need for
--impureor file system inputs.ruro commentedon Mar 12, 2025
@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:
You mentioned that
configurablePackagesisn't hardcoded in nix itself, but instead specified via flake schemas and showed the code forconfigurablePackagesSchema. However, I don't see this schema mentioned anywhere ingithub:edolstra/configurable-flake. Is adding custom schemas just currently unsupported or is it specified somewhere and I am just blind?I've noticed that at the end of the presentation, the "Try it out" section mentions
DeterminateSystems/nix-srcrather thanNixOS/nix: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! 🙏
svenssonaxel commentedon Jun 15, 2025
@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--argwould 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?nixos-discourse commentedon Oct 26, 2025
This issue has been mentioned on NixOS Discourse. There might be relevant details there:
https://discourse.nixos.org/t/organizing-your-nix-configuration-without-flakes/71009/17
yaitskov commentedon Feb 14, 2026
https://github.com/yaitskov/literal-flake-input
nixos-discourse commentedon Jul 11, 2026
This issue has been mentioned on NixOS Discourse. There might be relevant details there:
https://discourse.nixos.org/t/discussion-of-flakes-from-2025-nixos-community-survey-report/78853/11