Go to file
zimbatm b0d0c72b08
introduce flattenTree
2020-07-22 10:38:31 +02:00
example undate to latest flake 2020-07-07 14:01:39 +02:00
.gitignore better docs 2020-04-22 17:33:24 +02:00
LICENSE Initial commit 2020-04-11 13:21:35 +00:00
README.md introduce flattenTree 2020-07-22 10:38:31 +02:00
default.nix introduce flattenTree 2020-07-22 10:38:31 +02:00
flake.nix remove deprecated flake edition 2020-05-30 00:39:37 +02:00

README.md

flake-utils

STATUS: alpha

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

defaultSystems -> [<system>]

A list of all the systems supported by the nixpkgs project. Useful if you want add additional platforms:

eachSystem (defaultSystems ++ ["armv7l-linux"]) (system: { hello = 42; })

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:

eachSystem ["x86_64-linux"] (system: { hello = 42; })
# => { hello.x86_64-linux.hello = 42; }

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.

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:

flattenTree { hello = pkgs.hello; gitAndTools = pkgs.gitAndTools }

Returns:

{
  hello = «derivation»;
  gitAndTools_git = «derivation»;
  gitAndTools_hub = «derivation»;
  # ...
}

Example

Here is how it looks like in practice:

$ example/flake.nix as 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
      rec {
        packages.hello = pkgs.hello;
        defaultPackage = packages.hello;
        apps.hello = flake-utils.lib.mkApp { drv = packages.hello; };
        defaultApp = apps.hello;
      }
    );
}

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.