Skip to content

flake: add Nixpkgs default arguments as input - #160061

Open
roberth wants to merge 4 commits into
NixOS:masterfrom
hercules-ci:add-flake-default-arguments-as-input
Open

roberth wants to merge 4 commits into
NixOS:masterfrom
hercules-ci:add-flake-default-arguments-as-input

Conversation

@roberth

@roberth roberth commented Feb 14, 2022

Copy link
Copy Markdown
Member
Motivation for this change

Actually solve the problem of nixpkgs.config in a flake, and more.

Flakes are just functional programming with default arguments. The fact that everything has a default might throw you off, but it's still functional programming, where follows is apply. Enjoy!

Here's a flake producing skype from legacyPackages without the unfree error:

flake.nix

{
  description = "A very basic flake";

  inputs.nixpkgs.url = "github:hercules-ci/nixpkgs/add-flake-default-arguments-as-input";
  inputs.nixpkgs.inputs.parameters.follows = "parameters";
  inputs.parameters.url = "./parameters";

  outputs = { self, nixpkgs, ... }: {

    packages.x86_64-linux.skype = nixpkgs.legacyPackages.x86_64-linux.skype;

    defaultPackage.x86_64-linux = self.packages.x86_64-linux.skype;

  };
}

parameters/flake.nix

{
  outputs = { self }: {
    lib.pkgsParameters = {
      overlays = [ ];
      buildSystem = null; # same as host system
      config = {
        allowUnfree = true;
      };
    };
  };
}

Refs

Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 22.05 Release Notes (or backporting 21.11 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
    • (Release notes changes) Ran nixos/doc/manual/md-to-db.sh to update generated release notes
  • Fits CONTRIBUTING.md.

@otavio otavio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! That's nice :-D

@Ma27 Ma27 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some docs in e.g. the nixpkgs manual may be nice, but other than that, I like the idea %)

@github-actions github-actions Bot added the 8.has: documentation This PR adds or changes documentation label Mar 12, 2022
@roberth
roberth requested a review from Ma27 March 12, 2022 12:14
@ofborg ofborg Bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 0 This PR does not cause any packages to rebuild on Linux. labels Mar 12, 2022
@ncfavier
ncfavier requested a review from edolstra March 12, 2022 22:10
@Artturin
Artturin requested a review from pasqui23 April 24, 2022 00:11
Comment thread flake.nix
description = "A collection of packages for the Nix package manager";

outputs = { self }:
inputs.parameters.url = "github:NixOS/nixpkgs/default-parameters";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
inputs.parameters.url = "github:NixOS/nixpkgs/default-parameters";
inputs.parameters.url = ./default-params;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say this is a good suggestion, but I'm hesitant to apply it, as subflakes are quite broken still.

@viperML

viperML commented Jul 8, 2022

Copy link
Copy Markdown
Contributor

I am eagerly waiting for this (or any other similar solution) to be merged.

As mentioned in the PR, it would solve a common problem of flakes, that is to provide packages with unfree licenses. The current status is either to:

  1. Provide package outputs that cannot be built (bad)
  2. Instantiate our own nixpkgs, without any prompt of the user (bad)

A flake with unfree outputs could just bundle its default nixpkgs config, that allows for allowUnfree = true, while also being able to follow the user's config, eliminating problem 2.


Another usecase that comes to my mind is using config.contentAddressedByDefault = true. As a flake writer, I need to use nixpkgs.legacyPackages so my consumers can bring their own nixpkgs with their config. Even then, they would have to use an intermediate flake to wrap nixpkgs, such that it would import nixpkgs with its config, then re-export it, and then set the follows.

Consuming flake -> nixpkgs with contentAddressedByDefault -> My flake

In a similar fashion to https://github.com/numtide/nixpkgs-unfree , but for every combination of configuration options...

@edolstra

edolstra commented Jul 8, 2022

Copy link
Copy Markdown
Member

Strong 👎 on this. We should not invent yet another undiscoverable ad-hoc configuration mechanism to work around the lack of a configuration mechanism in flakes. Flake inputs are not intended to pass configuration data around and we should not start using them for that.

We should address the flake configuration issue at some point (probably after stabilising current flakes), but ideally in a standardized, discoverable way.

Comment thread flake.nix
description = "A collection of packages for the Nix package manager";

outputs = { self }:
inputs.parameters.url = "github:NixOS/nixpkgs/default-parameters";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This refers to the "default-params" branch of the nixpkgs repo, which doesn't exist AFAIK and whose flake.nix presumably is the top-level flake. Did you mean github:NixOS/nixpkgs?dir=default-params or something like that?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I created the branch. At the time, a branch seemed more suitable, because before the source tree abstraction branch, a relative path subflake still required a nar hash that matches the local file contents, whereas an input from a branch was more easily understood and more stable in terms of its implementation in the nix tool.

The default parameters were "empty" values like [ ] for overlays and { } for config, and the default behavior emerges from having empty "overrides". So the defaults for these values wouldn't ever have to change, and the branch would most likely have only one commit referencing a tiny tree that's unrelated to trees in the normal branches. Putting it in a separate repo may have been more appropriate.

@roberth

roberth commented Jul 8, 2022

Copy link
Copy Markdown
Member Author

We should not invent yet another undiscoverable ad-hoc configuration mechanism

I agree with this part. One of the valuable lessons of NixOS is that a simple language intended for package management is powerful enough to support all configuration needs. By restricting the configuration system to a small but powerful set of primitives, being the Nix language, we have gained a system with a small core that provides a consistent paradigm for configuration that allows the interoperation of both highly capable modules (NixOS) and easy to use configurations (configuration.nix).

the lack of a configuration mechanism in flakes

So one thing that NixOS teaches us is that function parameters, and by extension the attributes in them (config.*, etc) are suitable for configuration. We didn't need "configurable expressions" and we were better off without them, as their lack simplifies the language.

What are flake inputs if not parameters with defaults? The only thing about inputs that's really new is that their defaults come from a lock file, and this is great. As for the other aspects, they are a reinvention of concepts that already existed in the language, such as function application, but without interaction with critical other language features such as scoping.

What has happened is that in the name of standardization, we have let go of our ideal of a small language, and now we're starting to see the paradigm falling apart, in the form of a claim that "inputs" are not suitable for "configuration", whereas previously these were the same thing, interoperating in harmony with the rest of the language.

A warning from the Java world

To understand what Flakes are, we can decompose them into

  • a system for retrieving and locking dependencies
  • a set of CLI improvements
  • a dependency injection system

Dependency injection systems are the bread and butter of the Java world. These are systems that solve the "problem" of invoking the constructors of singleton-like objects and passing configuration parameters into them - ie exactly what flakes are doing. They have many frameworks to fulfill this task, but it is noteworthy that the Spring framework has gone full circle and reverted back to using regular code for this purpose, albeit annotated code.

Both the sheer number of implementations and the reversal to regular code are indications that dependency injection is not solvable without the occasional need for a programming language to specify what goes where.

If the JVM enforced a single dependency injection framework (like Nix does with the hardcoded call-flake.nix), neither the experimentation with frameworks, nor the reversal to "inject it yourself" would have been possible.

It's not too late to simplify flakes. It's still experimental.

I regret not taking part in the discussions and early design in 2018. I did not foresee that Flakes would be as invasive or that its design wouldn't be improved much from what was presented at that NixCon. My hindsight is 20/20.

@edolstra

edolstra commented Jul 8, 2022

Copy link
Copy Markdown
Member

So one thing that NixOS teaches us is that function parameters, and by extension the attributes in them (config.*, etc) are suitable for configuration.

I think what we've learned is that functions (at least as done in Nix) are not suitable for configuration since they lack discoverability and overridability. The NixOS module system is a lot better from that perspective.

To understand what Flakes are, we can decompose them into

a system for retrieving and locking dependencies
a set of CLI improvements
a dependency injection system

Flakes are intended as the first (specifying/locking repo dependencies). The CLI improvements (and conventions like standard flake outputs) are built on top of it, but are not part of the flake spec. It was not intended as a dependency injection system.

It's not too late to simplify flakes.

I don't think there is that much that can be simplified further, since the flake spec is already pretty minimal. The only major thing to reconsider is the input override mechanism, since that allows flake inputs to be (ab)used for dependency injection. For the flake standardization RFC, we could keep overrides as an experimental feature for the time being.

@roberth

roberth commented Jul 8, 2022

Copy link
Copy Markdown
Member Author

It was not intended as a dependency injection system.

Then it should not invoke the outputs function, and certainly not for the outputs of any of the inputs.

I don't think there is that much that can be simplified further [...] The only major thing to reconsider is the input override mechanism

If you don't invoke the outputs, the user can assemble their inputs' outputs in any way they like using the Nix language, making the input override and follows mechanisms redundant. Follows has been a source of bugs, and I'm still getting weird behavior when I update an input flakeref sometimes. Good riddance?

since the flake spec is already pretty minimal.

Where do I find the latest spec?

functions (at least as done in Nix) are not suitable for configuration [...] NixOS module system

Yet the module system fits within the functional paradigm, which illustrates the point that a small Nix is good and supports both ease of use (config modules) and fairly complicated transformations (functions in modules). The same can apply to a more minimal implementation of the flake goals; properly excluding dependency injection.

@edolstra

edolstra commented Jul 8, 2022

Copy link
Copy Markdown
Member

Where do I find the latest spec?

https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-flake.html#flake-format

Yeah, follows and overrides and their interaction has been buggy.

@stale stale Bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jan 7, 2023
@SomeoneSerge

Copy link
Copy Markdown
Contributor

Maybe a compromise would be to create a proxy flake repo under nix-community akin to nixpkgs-unfree, which would import nixpkgs with an overridable config. On one hand, this would read as less of an endorsement, on the other it'd further discourage users from treating flake inputs as paths (incl. callPackage "${nixpkgs}/pkgs/...).

I think @roberth's proposal is very important. The "flakes as paths" pattern is already being commonly abused. E.g., at the time of writing, github:ggerganov/llama.cpp imports Nixpkgs almost a dozen times with different configs to expose attributes for different CUDA and ROCm targets, because there isn't a flake-friendly alternative.

I also think it's a fair remark that since it isn't exactly trivial to treat flake.nix as a normal Nix function, flakes "as used in practice" are more than a way to pin dependencies. Flakes may not be optimal as a DI system, and that may be a reason not to promote them as such. However, if we're choosing between configs as flake inputs and flake inputs as paths, and there's still no prospect of a third option, I think we should choose the former.

@edolstra I wonder how you feel on the matter now that some time has passed?


Regarding the implementation and mirroring some comments from the nixos matrix: since flakes weren't designed to be a DI system, they aren't particularly strong in this role. E.g. if one wanted to use nixpkgs and flake-parts, and tried additionally to minimize the flake graph, they'd likely have a trouble making flake-parts' nixpkgs-lib use the same revision of nixpkgs as the parent flake.

@wegank wegank added the 2.status: merge conflict This PR has merge conflicts with the target branch label Mar 20, 2024
@stale stale Bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Mar 20, 2024
@wegank wegank added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jul 4, 2024
@nixos-discourse

Copy link
Copy Markdown

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

https://discourse.nixos.org/t/im-not-convinced-flake-inputs-should-exist/50636/16

@stale stale Bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Aug 18, 2024
@wegank wegank added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jan 1, 2025
@nixos-discourse

Copy link
Copy Markdown

@nixpkgs-ci nixpkgs-ci Bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Apr 29, 2026
@nixpkgs-ci nixpkgs-ci Bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.status: merge conflict This PR has merge conflicts with the target branch 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md 8.has: documentation This PR adds or changes documentation 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 0 This PR does not cause any packages to rebuild on Linux. 12.approvals: 1 This PR was reviewed and approved by one person.

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.