2020-04-11 15:21:35 +02:00
|
|
|
# flake-utils
|
2020-04-22 17:12:09 +02:00
|
|
|
|
2020-04-22 17:33:24 +02:00
|
|
|
**STATUS: alpha**
|
2020-04-22 17:12:09 +02:00
|
|
|
|
|
|
|
Pure Nix flake utility functions.
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-08-10 12:06:06 +02:00
|
|
|
### `allSystems -> [<system>]`
|
|
|
|
|
|
|
|
A list of all systems defined in nixpkgs. For a smaller list see `defaultSystems`
|
|
|
|
|
2020-04-22 17:33:24 +02:00
|
|
|
### `defaultSystems -> [<system>]`
|
|
|
|
|
2020-08-10 12:06:06 +02:00
|
|
|
The list of systems supported by nixpkgs and built by hydra.
|
2020-07-21 13:58:06 +02:00
|
|
|
Useful if you want add additional platforms:
|
|
|
|
|
|
|
|
```nix
|
|
|
|
eachSystem (defaultSystems ++ ["armv7l-linux"]) (system: { hello = 42; })
|
|
|
|
```
|
2020-04-22 17:33:24 +02:00
|
|
|
|
|
|
|
### `eachSystem -> [<system>] -> (<system> -> attrs)`
|
|
|
|
|
|
|
|
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
|
2020-06-30 18:19:09 +02:00
|
|
|
eachSystem ["x86_64-linux"] (system: { hello = 42; })
|
|
|
|
# => { hello.x86_64-linux.hello = 42; }
|
2020-08-10 12:06:06 +02: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 17:33:24 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
### `eachDefaultSystem -> (<system> -> attrs)`
|
|
|
|
|
|
|
|
`eachSystem` pre-populated with `defaultSystems`.
|
|
|
|
|
|
|
|
### `mkApp { drv, name ? drv.pname or drv.name, execPath ? drv.passthru.execPath or "/bin/${name}"`
|
|
|
|
|
|
|
|
A small utility that builds the structure expected by the special `apps` and `defaultApp` prefixes.
|
|
|
|
|
2020-07-22 10:38:25 +02:00
|
|
|
### `flattenTree -> attrs -> attrs`
|
|
|
|
|
|
|
|
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
|
|
|
|
recurseIntoAttrs) and only returns their derivations, with a flattened
|
|
|
|
key-space.
|
|
|
|
|
|
|
|
Eg:
|
|
|
|
```nix
|
|
|
|
flattenTree { hello = pkgs.hello; gitAndTools = pkgs.gitAndTools }
|
|
|
|
```
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
```nix
|
|
|
|
{
|
|
|
|
hello = «derivation»;
|
2020-07-22 11:35:14 +02:00
|
|
|
"gitAndTools/git" = «derivation»;
|
|
|
|
"gitAndTools/hub" = «derivation»;
|
2020-07-22 10:38:25 +02:00
|
|
|
# ...
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-04-22 17:33:24 +02:00
|
|
|
## Example
|
|
|
|
|
|
|
|
Here is how it looks like in practice:
|
|
|
|
|
|
|
|
[$ example/flake.nix](example/flake.nix) as nix
|
2020-04-22 17:12:09 +02:00
|
|
|
```nix
|
|
|
|
{
|
2020-04-22 17:33:24 +02:00
|
|
|
description = "Flake utils demo";
|
|
|
|
|
2020-07-07 14:01:39 +02:00
|
|
|
inputs.flake-utils.url = "github:numtide/flake-utils";
|
2020-04-22 17:33:24 +02:00
|
|
|
|
2020-07-07 14:01:39 +02:00
|
|
|
outputs = { self, nixpkgs, flake-utils }:
|
|
|
|
flake-utils.lib.eachDefaultSystem (system:
|
2020-04-22 17:12:09 +02:00
|
|
|
let pkgs = nixpkgs.legacyPackages.${system}; in
|
|
|
|
rec {
|
2020-04-22 17:33:24 +02:00
|
|
|
packages.hello = pkgs.hello;
|
|
|
|
defaultPackage = packages.hello;
|
2020-07-07 14:01:39 +02:00
|
|
|
apps.hello = flake-utils.lib.mkApp { drv = packages.hello; };
|
2020-04-22 17:33:24 +02:00
|
|
|
defaultApp = apps.hello;
|
|
|
|
}
|
2020-04-22 17:12:09 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
```
|
2020-07-20 15:57:39 +02:00
|
|
|
|
|
|
|
## Known issues
|
|
|
|
|
|
|
|
```
|
|
|
|
$ nix flake check
|
|
|
|
warning: unknown flake output 'lib'
|
|
|
|
```
|
|
|
|
|
|
|
|
nixpkgs is currently having the same issue so I assume that it will be
|
|
|
|
eventually standardized.
|
|
|
|
|