This commit is contained in:
zimbatm 2020-04-22 17:12:09 +02:00
parent 464b6ecb9b
commit e2ad62c2e3
No known key found for this signature in database
GPG Key ID: 71BAF6D40C1D63D7
3 changed files with 92 additions and 13 deletions

View File

@ -1,2 +1,40 @@
# flake-utils # flake-utils
Pure Nix flake utility functions
**STATUS: WIP**
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
`flake.nix`
```nix
{
edition = 201909;
description = "My flake";
inputs = {
utils = { type = "github"; owner = "numtide"; repo = "flake-utils"; };
};
outputs = { self, nixpkgs, utils }:
utils.eachDefaultSystem (system:
let pkgs = nixpkgs.legacyPackages.${system}; in
rec {
packages = {
my-app = pkgs.callPackage ./my-app.nix {};
};
defaultPackage = package.my-app;
apps = {
my-app = flake.mkApp packages.my-app;
};
defaultApp = apps.my-app;
};
);
}
```

View File

@ -1,28 +1,41 @@
rec { let
# copied from <nixpkgs/lib> # copied from <nixpkgs/lib>
genAttrs = names: f: genAttrs = names: f:
builtins.listToAttrs (map (n: { name = n; value = f n; }) names); builtins.listToAttrs (map (n: { name = n; value = f n; }) names);
mapAttrsToList = f: attrs:
map (name: f name attrs.${name}) (builtins.attrNames attrs);
# The list of systems supported by nixpkgs and hydra # The list of systems supported by nixpkgs and hydra
supportedSystems = [ defaultSystems = [
"aarch64-linux" "aarch64-linux"
"i686-linux" "i686-linux"
"x86_64-darwin" "x86_64-darwin"
"x86_64-linux" "x86_64-linux"
]; ];
# Returns an attribute set with all the supported systems as keys and the # eachSystem using defaultSystems
# output of the passed function with each system passed to it. eachDefaultSystem = eachSystem defaultSystems;
# Builds a map from <attr>=value to <system>.<attr>=value for each system.
# #
# This is useful in the flake outputs because the outputs return static sets
# that map to the different systems.
# #
# Example: eachSystem = systems: f:
# forAllSupported (x: null) let
# > { aarch64-linux = null; i686-linux = null; x86_64-darwin = null; op = attrs: system:
# > x86_64-linux = null; } let
# (system -> attrs) -> attrs ret = f system;
forAllSupported = genAttrs supportedSystems; opt = attrs: key:
attrs //
{
${key} = (attrs.${key} or {}) // { ${system} = ret.${key}; };
}
;
in
builtins.foldl' op attrs (builtins.attrNames ret);
in
builtins.foldl' op {} systems
;
# Returns the structure used by `nix app` # Returns the structure used by `nix app`
mkApp = mkApp =
@ -34,4 +47,12 @@ rec {
type = "app"; type = "app";
program = "${drv}${exePath}"; program = "${drv}${exePath}";
}; };
in
{
inherit
defaultSystems
eachDefaultSystem
eachSystem
mkApp
;
} }

20
example/flake.nix Normal file
View File

@ -0,0 +1,20 @@
{
description = "Flake utils demo";
edition = 201909;
inputs.utils = {
type = "git";
uri = "file:///home/zimbatm/go/src/github.com/zimbatm/flake-utils";
};
outputs = { self, nixpkgs, utils }:
utils.lib.eachDefaultSystem (system:
let pkgs = nixpkgs.legacyPackages.${system}; in
rec {
packages.hello = pkgs.hello;
defaultPackage = packages.hello;
apps.hello = utils.lib.mkApp packages.hello;
defaultApp = apps.hello;
}
);
}