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

Add meld (#99)

Meld allows you to break up a Nix Flake into parts while using the
same inputs across all of them.  This is useful for splitting up large
flakes which are common in monorepos.
This commit is contained in:
Javed Mohamed 2023-06-25 16:15:56 +00:00 committed by GitHub
parent abfb11bd1a
commit dbabf0ca0c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 0 deletions

View file

@ -113,6 +113,12 @@ eachSystem allSystems (system: { hello = 42; })
}
```
### `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.
### `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.

74
lib.nix
View file

@ -128,6 +128,79 @@ let
# }
filterPackages = import ./filterPackages.nix { inherit allSystems; };
# Meld merges subflakes using common inputs. Useful when you want
# to split up a large flake with many different components into more
# manageable parts.
#
# For example:
#
# {
# inputs = {
# flutils.url = "github:numtide/flake-utils";
# nixpkgs.url = "github:nixos/nixpkgs";
# };
# outputs = inputs@{ flutils, ... }: flutils.lib.meld inputs [
# ./nix/packages
# ./nix/hardware
# ./nix/overlays
# # ...
# ];
# }
#
# Where ./nix/packages/default.nix looks like just the output
# portion of a flake.
#
# { flutils, nixpkgs, ... }: flutils.lib.eachDefaultSystem (system:
# let pkgs = import nixpkgs { inherit system; }; in
# {
# packages = {
# foo = ...;
# bar = ...;
# # ...
# };
# }
# )
#
# You can also use meld within the subflakes to further subdivide
# your flake into a tree like structure. For example,
# ./nix/hardware/default.nix might look like:
#
# inputs@{ flutils, ... }: flutils.lib.meld inputs [
# ./foobox.nix
# ./barbox.nix
# ]
meld = let
# Pulled from nixpkgs.lib
recursiveUpdateUntil =
# Predicate, taking the path to the current attribute as a list of strings for attribute names, and the two values at that path from the original arguments.
pred:
# Left attribute set of the merge.
lhs:
# Right attribute set of the merge.
rhs:
let
f = attrPath:
builtins.zipAttrsWith (n: values:
let here = attrPath ++ [ n ];
in if builtins.length values == 1
|| pred here (builtins.elemAt values 1) (builtins.head values) then
builtins.head values
else
f here values);
in f [ ] [ rhs lhs ];
# Pulled from nixpkgs.lib
recursiveUpdate =
# Left attribute set of the merge.
lhs:
# Right attribute set of the merge.
rhs:
recursiveUpdateUntil (path: lhs: rhs: !(builtins.isAttrs lhs && builtins.isAttrs rhs)) lhs
rhs;
in inputs:
builtins.foldl' (output: subflake:
recursiveUpdate output (import subflake inputs)) { };
# Returns the structure used by `nix app`
mkApp =
{ drv
@ -156,6 +229,7 @@ let
eachSystemMap
filterPackages
flattenTree
meld
mkApp
simpleFlake
system