2024-11-13 21:27:16 +00:00
|
|
|
<div align="center">
|
|
|
|
|
2020-04-11 13:21:35 +00:00
|
|
|
# flake-utils
|
2020-04-22 15:12:09 +00:00
|
|
|
|
2024-11-13 21:27:16 +00:00
|
|
|
<img src="flake-utils.svg" height="150"/>
|
|
|
|
|
|
|
|
**Pure Nix flake utility functions.**
|
|
|
|
|
|
|
|
*A <a href="https://numtide.com/">numtide</a> project.*
|
|
|
|
|
|
|
|
<p>
|
|
|
|
<a href="https://github.com/numtide/flake-utils/actions/workflows/nix.yml"><img src="https://github.com/numtide/flake-utils/actions/workflows/nix.yml/badge.svg"/></a>
|
|
|
|
<a href="https://app.element.io/#/room/#home:numtide.com"><img src="https://img.shields.io/badge/Support-%23numtide-blue"/></a>
|
|
|
|
</p>
|
2020-04-22 15:12:09 +00:00
|
|
|
|
2024-11-13 21:27:16 +00:00
|
|
|
</div>
|
2020-04-22 15:12:09 +00:00
|
|
|
|
|
|
|
The goal of this project is to build a collection of pure Nix functions that don't
|
|
|
|
depend on nixpkgs, and that are useful in the context of writing other Nix
|
|
|
|
flakes.
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
2023-04-09 10:30:52 +00:00
|
|
|
### `system :: { system = system, ... }`
|
2022-01-20 17:42:55 +00:00
|
|
|
|
2023-04-09 10:30:52 +00:00
|
|
|
A map from system to system built from `allSystems`:
|
|
|
|
```nix
|
|
|
|
system = {
|
|
|
|
x86_64-linux = "x86_64-linux";
|
|
|
|
x86_64-darwin = "x86_64-darwin";
|
|
|
|
...
|
|
|
|
}
|
|
|
|
```
|
|
|
|
It's mainly useful to
|
|
|
|
detect typos and auto-complete if you use [rnix-lsp](https://github.com/nix-community/rnix-lsp).
|
2024-09-11 18:43:21 +00:00
|
|
|
|
2023-04-09 10:30:52 +00:00
|
|
|
Eg: instead of typing `"x86_64-linux"`, use `system.x86_64-linux`.
|
2022-01-20 17:42:55 +00:00
|
|
|
|
|
|
|
|
2023-04-09 10:30:52 +00:00
|
|
|
### `allSystems :: [<system>]`
|
2020-08-10 10:06:06 +00:00
|
|
|
|
2023-04-09 10:30:52 +00:00
|
|
|
A list of all systems defined in nixpkgs. For a smaller list see `defaultSystems`.
|
2020-08-10 10:06:06 +00:00
|
|
|
|
2023-04-11 08:47:17 +00:00
|
|
|
### `defaultSystems :: [<system>]`
|
2020-04-22 15:33:24 +00:00
|
|
|
|
2023-04-11 08:47:17 +00:00
|
|
|
The list of systems to use in `eachDefaultSystem` and `simpleFlake`.
|
2020-07-21 11:58:06 +00:00
|
|
|
|
2023-04-11 08:47:17 +00:00
|
|
|
The default values are `["x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin"]`.
|
|
|
|
|
|
|
|
It's possible to override and control that list by changing the `systems` input of this flake.
|
|
|
|
|
|
|
|
Eg (in your `flake.nix`):
|
|
|
|
|
|
|
|
```nix
|
|
|
|
{
|
|
|
|
# 1. Defined a "systems" inputs that maps to only ["x86_64-linux"]
|
|
|
|
inputs.systems.url = "github:nix-systems/x86_64-linux";
|
|
|
|
|
2023-07-11 09:46:48 +00:00
|
|
|
inputs.flake-utils.url = "github:numtide/flake-utils";
|
2023-04-11 08:47:17 +00:00
|
|
|
# 2. Override the flake-utils default to your version
|
|
|
|
inputs.flake-utils.inputs.systems.follows = "systems";
|
|
|
|
|
|
|
|
outputs = { self, flake-utils, ... }:
|
|
|
|
# Now eachDefaultSystem is only using ["x86_64-linux"], but this list can also
|
|
|
|
# further be changed by users of your flake.
|
|
|
|
flake-utils.lib.eachDefaultSystem (system: {
|
|
|
|
# ...
|
|
|
|
});
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
For more details in this pattern, see: <https://github.com/nix-systems/nix-systems>.
|
2020-04-22 15:33:24 +00:00
|
|
|
|
2023-04-09 10:30:52 +00:00
|
|
|
### `eachSystem :: [<system>] -> (<system> -> attrs)`
|
2020-04-22 15:33:24 +00:00
|
|
|
|
|
|
|
A common case is to build the same structure for each system. Instead of
|
|
|
|
building the hierarchy manually or per prefix, iterate over each systems and
|
|
|
|
then re-build the hierarchy.
|
|
|
|
|
|
|
|
Eg:
|
|
|
|
|
|
|
|
```nix
|
2022-01-20 17:42:55 +00:00
|
|
|
eachSystem [ system.x86_64-linux ] (system: { hello = 42; })
|
2020-11-14 16:09:53 +00:00
|
|
|
# => { hello = { x86_64-linux = 42; }; }
|
2020-08-10 10:06:06 +00:00
|
|
|
eachSystem allSystems (system: { hello = 42; })
|
|
|
|
# => {
|
|
|
|
hello.aarch64-darwin = 42,
|
|
|
|
hello.aarch64-genode = 42,
|
|
|
|
hello.aarch64-linux = 42,
|
|
|
|
...
|
|
|
|
hello.x86_64-redox = 42,
|
|
|
|
hello.x86_64-solaris = 42,
|
|
|
|
hello.x86_64-windows = 42
|
|
|
|
}
|
2020-04-22 15:33:24 +00:00
|
|
|
```
|
|
|
|
|
2024-09-11 18:13:13 +00:00
|
|
|
### `eachSystemPassThrough :: [<system>] -> (<system> -> attrs)`
|
|
|
|
|
|
|
|
Unlike `eachSystem`, this function does not inject the `${system}` key by merely
|
|
|
|
providing the system argument to the function.
|
|
|
|
|
2023-04-09 10:30:52 +00:00
|
|
|
### `eachDefaultSystem :: (<system> -> attrs)`
|
2020-04-22 15:33:24 +00:00
|
|
|
|
|
|
|
`eachSystem` pre-populated with `defaultSystems`.
|
|
|
|
|
2020-08-23 13:28:05 +00:00
|
|
|
#### Example
|
|
|
|
|
|
|
|
[$ examples/each-system/flake.nix](examples/each-system/flake.nix) as nix
|
|
|
|
```nix
|
|
|
|
{
|
|
|
|
description = "Flake utils demo";
|
|
|
|
|
|
|
|
inputs.flake-utils.url = "github:numtide/flake-utils";
|
|
|
|
|
|
|
|
outputs = { self, nixpkgs, flake-utils }:
|
|
|
|
flake-utils.lib.eachDefaultSystem (system:
|
|
|
|
let pkgs = nixpkgs.legacyPackages.${system}; in
|
2023-03-15 17:33:47 +00:00
|
|
|
{
|
|
|
|
packages = rec {
|
2020-08-23 13:28:05 +00:00
|
|
|
hello = pkgs.hello;
|
2023-03-15 17:33:47 +00:00
|
|
|
default = hello;
|
|
|
|
};
|
|
|
|
apps = rec {
|
|
|
|
hello = flake-utils.lib.mkApp { drv = self.packages.${system}.hello; };
|
|
|
|
default = hello;
|
2020-08-23 13:28:05 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2024-09-11 18:13:13 +00:00
|
|
|
### `eachDefaultSystemPassThrough :: (<system> -> attrs)`
|
|
|
|
|
|
|
|
`eachSystemPassThrough` pre-populated with `defaultSystems`.
|
|
|
|
|
|
|
|
#### Example
|
|
|
|
|
|
|
|
```nix
|
|
|
|
inputs.flake-utils.lib.eachDefaultSystem (system: {
|
|
|
|
checks./*<SYSTEM>.*/"<CHECK>" = /* ... */;
|
|
|
|
devShells./*<SYSTEM>.*/"<DEV_SHELL>" = /* ... */;
|
|
|
|
packages./*<SYSTEM>.*/"<PACKAGE>" = /* ... */;
|
|
|
|
})
|
|
|
|
// inputs.flake-utils.lib.eachDefaultSystemPassThrough (system: {
|
|
|
|
homeConfigurations."<HOME_CONFIGURATION>" = /* ... */;
|
|
|
|
nixosConfigurations."<NIXOS_CONFIGURATION>" = /* ... */;
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
2023-06-25 16:15:56 +00:00
|
|
|
### `meld :: attrs -> [ path ] -> attrs`
|
|
|
|
|
|
|
|
Meld merges subflakes using common inputs. Useful when you want to
|
|
|
|
split up a large flake with many different components into more
|
|
|
|
manageable parts.
|
|
|
|
|
2020-09-15 22:45:23 +00:00
|
|
|
### `mkApp { drv, name ? drv.pname or drv.name, exePath ? drv.passthru.exePath or "/bin/${name}"`
|
2020-04-22 15:33:24 +00:00
|
|
|
|
|
|
|
A small utility that builds the structure expected by the special `apps` and `defaultApp` prefixes.
|
|
|
|
|
2020-08-23 13:28:05 +00:00
|
|
|
|
2023-04-09 10:30:52 +00:00
|
|
|
### `flattenTree :: attrs -> attrs`
|
2020-07-22 08:38:25 +00:00
|
|
|
|
|
|
|
Nix flakes insists on having a flat attribute set of derivations in
|
|
|
|
various places like the `packages` and `checks` attributes.
|
|
|
|
|
|
|
|
This function traverses a tree of attributes (by respecting
|
2024-01-15 09:00:34 +00:00
|
|
|
[recurseIntoAttrs](https://noogle.dev/f/lib/recurseIntoAttrs)) and only returns their derivations, with a flattened
|
2020-07-22 08:38:25 +00:00
|
|
|
key-space.
|
|
|
|
|
|
|
|
Eg:
|
|
|
|
```nix
|
|
|
|
flattenTree { hello = pkgs.hello; gitAndTools = pkgs.gitAndTools }
|
|
|
|
```
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
```nix
|
|
|
|
{
|
|
|
|
hello = «derivation»;
|
2020-07-22 09:35:14 +00:00
|
|
|
"gitAndTools/git" = «derivation»;
|
|
|
|
"gitAndTools/hub" = «derivation»;
|
2020-07-22 08:38:25 +00:00
|
|
|
# ...
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2023-04-09 10:30:52 +00:00
|
|
|
### `simpleFlake :: attrs -> attrs`
|
2020-08-23 13:28:05 +00:00
|
|
|
|
|
|
|
This function should be useful for most common use-cases where you have a
|
|
|
|
simple flake that builds a package. It takes nixpkgs and a bunch of other
|
|
|
|
parameters and outputs a value that is compatible as a flake output.
|
|
|
|
|
|
|
|
Input:
|
|
|
|
```nix
|
|
|
|
{
|
|
|
|
# pass an instance of self
|
|
|
|
self
|
|
|
|
, # pass an instance of the nixpkgs flake
|
|
|
|
nixpkgs
|
|
|
|
, # we assume that the name maps to the project name, and also that the
|
|
|
|
# overlay has an attribute with the `name` prefix that contains all of the
|
|
|
|
# project's packages.
|
|
|
|
name
|
|
|
|
, # nixpkgs config
|
|
|
|
config ? { }
|
|
|
|
, # pass either a function or a file
|
|
|
|
overlay ? null
|
|
|
|
, # use this to load other flakes overlays to supplement nixpkgs
|
|
|
|
preOverlays ? [ ]
|
|
|
|
, # maps to the devShell output. Pass in a shell.nix file or function.
|
|
|
|
shell ? null
|
|
|
|
, # pass the list of supported systems
|
2023-04-08 09:39:05 +00:00
|
|
|
systems ? [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ]
|
2020-08-23 13:28:05 +00:00
|
|
|
}: null
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Example
|
2020-04-22 15:33:24 +00:00
|
|
|
|
|
|
|
Here is how it looks like in practice:
|
|
|
|
|
2020-08-23 13:28:05 +00:00
|
|
|
[$ examples/simple-flake/flake.nix](examples/simple-flake/flake.nix) as nix
|
2020-04-22 15:12:09 +00:00
|
|
|
```nix
|
|
|
|
{
|
2020-04-22 15:33:24 +00:00
|
|
|
description = "Flake utils demo";
|
|
|
|
|
2020-12-29 12:59:39 +00:00
|
|
|
inputs.flake-utils.url = "github:numtide/flake-utils";
|
2020-04-22 15:33:24 +00:00
|
|
|
|
2020-07-07 12:01:39 +00:00
|
|
|
outputs = { self, nixpkgs, flake-utils }:
|
2020-08-23 13:28:05 +00:00
|
|
|
flake-utils.lib.simpleFlake {
|
|
|
|
inherit self nixpkgs;
|
|
|
|
name = "simple-flake";
|
|
|
|
overlay = ./overlay.nix;
|
|
|
|
shell = ./shell.nix;
|
|
|
|
};
|
2020-04-22 15:12:09 +00:00
|
|
|
}
|
|
|
|
```
|
2023-08-23 12:03:29 +00:00
|
|
|
|
|
|
|
## Commercial support
|
|
|
|
|
|
|
|
Looking for help or customization?
|
|
|
|
|
|
|
|
Get in touch with Numtide to get a quote. We make it easy for companies to
|
|
|
|
work with Open Source projects: <https://numtide.com/contact>
|