1
0
Fork 0
mirror of https://github.com/numtide/flake-utils.git synced 2024-12-14 11:47:31 +00:00
flake-utils/README.md

208 lines
5.4 KiB
Markdown
Raw Normal View History

2020-04-11 13:21:35 +00:00
# flake-utils
2020-04-22 15:12:09 +00:00
**STATUS: stable**
2020-04-22 15:12:09 +00: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
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).
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>]`
2023-04-09 10:30:52 +00:00
A list of all systems defined in nixpkgs. For a smaller list see `defaultSystems`.
### `defaultSystems :: [<system>]`
2020-04-22 15:33:24 +00:00
The list of systems to use in `eachDefaultSystem` and `simpleFlake`.
2020-07-21 11:58:06 +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";
# 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; }; }
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
```
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
{
packages = rec {
2020-08-23 13:28:05 +00:00
hello = pkgs.hello;
default = hello;
};
apps = rec {
hello = flake-utils.lib.mkApp { drv = self.packages.${system}.hello; };
default = hello;
2020-08-23 13:28:05 +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
recurseIntoAttrs) and only returns their derivations, with a flattened
key-space.
Eg:
```nix
flattenTree { hello = pkgs.hello; gitAndTools = pkgs.gitAndTools }
```
Returns:
```nix
{
hello = «derivation»;
"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
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";
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
}
```
## 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>