1
0
Fork 0
mirror of https://github.com/numtide/flake-utils.git synced 2025-03-13 20:28:42 +00:00
Pure Nix flake utility functions [maintainer=@zimbatm]
Find a file
Sergey Vlasov 3b6a41d794 filterPackages: Do not use meta.hydraPlatforms for filtering
The meta.hydraPlatforms attribute was never intended to mark the package
broken; actually lots of packages have `meta.hydraPlatforms = []` while
being perfectly usable.  Typical reasons for disabling Hydra builds are:

  - the package build process is so trivial that caching the build
    results on Hydra won't be useful (this often applies to wrapper
    packages, or some packages which contain just prebuilt data or
    binaries);

  - the package build process exceeds Hydra limits;

  - the package is impure and depends on some proprietary software
    installed on the build host (this is the case with some
    Darwin-specific packages that require Xcode to build);

  - the package license does not allow redistribution of binaries (which
    also may need to be modified to work with Nix, and many proprietary
    licenses don't allow redistribution of such modified binaries);
    although this should normally be handled with meta.unfree.

Especially the first case (wrapper packages) hits some important
packages (firefox, neovim).

Remove the usage of meta.hydraPlatforms from the filterPackages code, so
that the filtering performed by that function would match the actual
platform restriction of packages (meta.platforms and meta.badPlatforms).
2022-06-24 00:07:55 +03:00
.github Merge pull request #56 from numtide/dependabot/github_actions/actions/checkout-3 2022-05-14 20:23:30 +01:00
examples Fix check-utils template directory name (#70) 2022-05-30 08:55:45 +02:00
.gitignore better docs 2020-04-22 17:33:24 +02:00
check-utils.nix Sanitize the final derivation name to ensure length limits are respected (#66) 2022-05-16 22:32:57 +02:00
default.nix feat: add convenience eachDefaultSystemMap (#60) 2022-05-17 10:27:56 +02:00
filterPackages.nix filterPackages: Do not use meta.hydraPlatforms for filtering 2022-06-24 00:07:55 +03:00
flake.nix expose examples as templates (#63) 2022-05-17 09:58:38 +02:00
flattenTree.nix flattenTree: use / as the key separator 2020-07-22 11:35:16 +02:00
LICENSE Initial commit 2020-04-11 13:21:35 +00:00
README.md add system map for convenience (#55) 2022-01-20 18:42:55 +01:00
simpleFlake.nix use defaultSystems in simpleFlake.nix 2022-05-12 18:28:16 +02:00

flake-utils

Support room on Matrix

STATUS: stable

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

system -> (<system> -> <system>)

A map from system to system built from allSystems. It's mainly useful to detect typos and auto-complete if you use rnix-lsp.

Eg: instead of typing "x86_64-linux", use system.x86_64-linux

allSystems -> [<system>]

A list of all systems defined in nixpkgs. For a smaller list see defaultSystems

defaultSystems -> [<system>]

The list of systems supported by nixpkgs and built by hydra. Useful if you want add additional platforms:

eachSystem (defaultSystems ++ [system.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 [ system.x86_64-linux ] (system: { hello = 42; })
# => { 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
}

eachDefaultSystem -> (<system> -> attrs)

eachSystem pre-populated with defaultSystems.

Example

$ examples/each-system/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 = flake-utils.lib.flattenTree {
          hello = pkgs.hello;
          gitAndTools = pkgs.gitAndTools;
        };
        defaultPackage = packages.hello;
        apps.hello = flake-utils.lib.mkApp { drv = packages.hello; };
        defaultApp = apps.hello;
      }
    );
}

mkApp { drv, name ? drv.pname or drv.name, exePath ? drv.passthru.exePath 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»;
  # ...
}

simpleFlake -> attrs -> attrs

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:

{
  # 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 ? [ system.x86_64-linux ]
}: null

Example

Here is how it looks like in practice:

$ examples/simple-flake/flake.nix as nix

{
  description = "Flake utils demo";

  inputs.flake-utils.url = "github:numtide/flake-utils";

  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.simpleFlake {
      inherit self nixpkgs;
      name = "simple-flake";
      overlay = ./overlay.nix;
      shell = ./shell.nix;
    };
}

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.