Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flake: expose the list of supported systems #228

Merged
merged 1 commit into from
Apr 9, 2023
Merged

flake: expose the list of supported systems #228

merged 1 commit into from
Apr 9, 2023

Conversation

zimbatm
Copy link
Member

@zimbatm zimbatm commented Apr 6, 2023

This commit introduces a new convention that allows consumers of a flake to control systems the flake is built with.

One of the core issues of flakes is that the list of systems that a flake is evaluated with is internal to the flake. Some projects can technically build with more platforms, but to change this, the user has to fork or submit a patch to change the list. Conversely, as a consumer of a flake, you might not care of evaluating the flake with all of the systems, and that list cannot be reduced.

To work around that issue, we introduce a new convention:

  1. If the systems input exists, it MUST point to a nix file.
  2. That file SHOULD be named flake.systems.nix.
  3. That file MUST contain a list of supported systems (list of strings)
  4. That input MUST be used by the flake to iterate over the supported systems (see this commit as an example)

With this convention in place, consumers of the flake can now override the list of systems using the input "follows" mechanism like so:

inputs.systems.url = "path:./flake.systems.nix";
inputs.systems.flake = false;
inputs.treefmt.url = "github:numtide/treefmt";
inputs.treefmt.inputs.follows.systems = "systems";

Invented with the help of @bb010g in numtide/flake-utils#84

@Mic92
Copy link
Member

Mic92 commented Apr 7, 2023

This one seems to be an issue:

$ nix flake show github:/numtide/treefmt/flake-systems
error: cannot write modified lock file of flake 'github:numtide/treefmt/flake-systems' (use '--no-write-lock-file' to ignore)
(use '--show-trace' to show detailed location information)

You are basically breaking nix run and friends this way.

@zimbatm zimbatm force-pushed the flake-systems branch 2 times, most recently from aa7acce to 3f505ef Compare April 7, 2023 08:42
@zimbatm
Copy link
Member Author

zimbatm commented Apr 7, 2023

Thanks, the lock file wasn't up to date for some reason. It's working for me now.

Another fun thing that this unlocks:

$ nix flake show github:/numtide/treefmt/flake-systems 
github:numtide/treefmt/3f505ef27a660f1f5051200103c2849b450e88ef
├───apps
│   ├───aarch64-darwin
│   ├───aarch64-linux
│   ├───x86_64-darwin
│   └───x86_64-linux
<snip>
$ echo '["x86_64-linux"]' > systems.nix
$ nix flake show github:/numtide/treefmt/flake-systems --override-input systems "path:./systems.nix"
warning: not writing modified lock file of flake 'github:numtide/treefmt/flake-systems':
• Updated input 'systems':
    'path:./flake.systems.nix?lastModified=1&narHash=sha256-FqEFZR55m1uKlWkReR%2f20uF8fph2G%2fUDeK7zzXB5rBI=' (1970-01-01)
  → 'path:./systems.nix?lastModified=1680857188&narHash=sha256-kAigsS+U8wSLkOJ79mOXliuDWLQXp%2frnUZbhQTqc3BM=' (2023-04-07)
github:numtide/treefmt/3f505ef27a660f1f5051200103c2849b450e88ef
├───apps
│   └───x86_64-linux
├───checks
│   └───x86_64-linux
<snip>

@zimbatm
Copy link
Member Author

zimbatm commented Apr 7, 2023

In https://github.com/zimbatm/exp-flake-systems I tried taking the idea even further and introducing it on the flake registry level. Too bad, flake-registry only supports referencing flakes :)

@srid
Copy link
Contributor

srid commented Apr 7, 2023

Another fun thing that this unlocks:

Can we avoid having to create systems.nix? What about creating a Github org, then creating per-system repos, and then doing:

nix flake show github:/numtide/treefmt/flake-systems \
  --override-input systems "github:nix-systems/aarch64-darwin"

🧌

@srid
Copy link
Contributor

srid commented Apr 7, 2023

In https://github.com/zimbatm/exp-flake-systems I tried taking the idea even further and introducing it on the flake registry level. Too bad, flake-registry only supports referencing flakes :)

What if systems is an actual flake, though?

{
  outputs = _: {
    systems = [ "x86_64-linux" ];
  };
}

However, why not generalize all of this to be a flake-parts module?

{
  outputs = _: {
    flakeModule.systems = [ "x86_64-linux" ];
  };
}

cc @roberth and cf. hercules-ci/flake-parts#25

srid added a commit to srid/check-flake that referenced this pull request Apr 7, 2023
@zimbatm
Copy link
Member Author

zimbatm commented Apr 7, 2023

Can we avoid having to create systems.nix?

Not for all combinations, but we might be able to provide common defaults.

The real solution would be to add built-in support to Nix itself.

@zimbatm
Copy link
Member Author

zimbatm commented Apr 7, 2023

I updated https://github.com/zimbatm/exp-flake-systems to make it an empty flake. You're still supposed to import it, so it's compatible with the local flake.systems.nix pattern.

flake-parts is pretty heavy so I try to avoid it on flakes that are designed to be consumed by other flakes

@bb010g
Copy link

bb010g commented Apr 7, 2023

With regards to https://github.com/zimbatm/exp-flake-systems, I'd really like to see a NixOS/systems or similar that Nixpkgs itself takes as a flake input and uses in place of their current https://github.com/NixOS/nixpkgs/blob/db73cdeb180ba079bf91e9175fe8d72197a0156c/lib/systems/flake-systems.nix (Nixpkgs's version of systems/defaultSystems). I do think this input needs to also provide allSystems (inputs.systems + "/all.nix" if it's a directory / actual flake?) because if utilities like flake-utils.lib.system continue to be around and are used for validation, downstreams need to be able to override them when introducing new systems. Also, if NixOS hosted this, allSystems becoming out of sync with nixpkgs.lib.platforms.all would be a proper bug. (At the very least, I'd like to see github:nix-community/systems if we can't get github:NixOS/systems.)

@zimbatm
Copy link
Member Author

zimbatm commented Apr 8, 2023

It's starting to take shape quite nicely. I move the experiment to https://github.com/numtide/flake-systems.

@zimbatm
Copy link
Member Author

zimbatm commented Apr 8, 2023

Now it has its own full github org to document the pattern and host common system combos: https://github.com/nix-systems/nix-systems

This is a new convention that lets consumers of the flake control what
systems this flake will be built with.

As a consumer you can pass your own list of systems, by overriding the
inputs. Eg:

```
inputs.systems.url = "path:./flake.systems.nix";
inputs.systems.flake = false;
inputs.treefmt.url = "github:numtide/treefmt";
inputs.treefmt.inputs.follows.systems = "systems";
```

Invented with the help of bb010g in numtide/flake-utils#84

See <https://github.com/nix-systems/nix-systems>
@zimbatm
Copy link
Member Author

zimbatm commented Apr 9, 2023

bors merge

zimbatm added a commit to zimbatm/flake-modules-core that referenced this pull request Apr 9, 2023
With this change, it becomes possible to modify the list of systems that
a flake is using, using only the flake override mechanisms.

See <numtide/treefmt#228> and
<https://github.com/nix-systems/nix-systems>.
@bors bors bot merged commit a2dcb65 into main Apr 9, 2023
@bors bors bot deleted the flake-systems branch April 9, 2023 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants