Skip to content

Commit

Permalink
Add lib.mkFlake (#86)
Browse files Browse the repository at this point in the history
Provide the nixos/nix-darwin/home-manager autowiring feature to
flake-parts as well.
  • Loading branch information
srid authored Oct 20, 2024
1 parent 26dea07 commit 4f42628
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
21 changes: 21 additions & 0 deletions doc/src/autowiring.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ Each of these are wired to the corresponding flake output, as indicated in the b
| `modules/flake-parts/foo.nix` | `flakeModules.foo` |
| `overlays/foo.nix` | `overlays.foo` |

## flake-parts

Autowiring is also provided if you use just flake-parts, via the `lib.mkFlake` function. In your top-level flake.nix, you only need to define your `outputs` as follows:

```nix
{
inputs = ...;
outputs = inputs:
inputs.nixos-unified.lib.mkFlake
{ inherit inputs; root = ./.; };
}
```

This will,

- Auto-import flake-parts modules under either `./nix/modules/flake-parts` or `./modules/flake-parts` (whichever exists)
- Use a sensible default for `systems` which can be overriden.
- Pass `root` as top-level module args, as a non-recursive way of referring to the path of the flake (without needing `inputs.self`).

See [srid/haskell-template's flake.nix](https://github.com/srid/haskell-template/blob/master/flake.nix) for a ready example.

[^default]: This path could as well be `configurations/nixos/foo/default.nix`. Likewise for other output types.

[^hm-pkgs]: Why `legacyPackages`? Because, creating a home-manager configuration [requires `pkgs`](https://github.com/srid/nixos-unified/blob/47a26bc9118d17500bbe0c4adb5ebc26f776cc36/nix/modules/flake-parts/lib.nix#L97). See <https://github.com/nix-community/home-manager/issues/3075>
24 changes: 24 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,30 @@
# For backwards compat only
flakeModule = flakeModules.default;

# Like flake-parts mkFlake, but auto-imports modules/flake-parts, consistent with autowiring feature.
#
# Looks under either nix/modules/flake-parts or modules/flake-parts for modules to import. `systems` is set to a default value. `root` is passed as top-level module args (as distinct from `inputs.self` the use of which can lead to infinite recursion).
lib.mkFlake =
{ inputs
, root
, systems ? [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ]
}:
inputs.flake-parts.lib.mkFlake { inherit inputs; } {
inherit systems;
_module.args = { inherit root; };
imports = with builtins;
if pathExists "${root}/nix/modules/flake-parts" then
map
(fn: "${root}/nix/modules/flake-parts/${fn}")
(attrNames (readDir (root + /nix/modules/flake-parts)))
else if pathExists "${root}/modules/flake-parts.nix" then
map
(fn: "${root}/modules/flake-parts/${fn}")
(attrNames (readDir (root + /modules/flake-parts)))
else
throw "Neither modules/flake-parts nor nix/modules/flake-parts exist";
};

templates =
let
tmplPath = path: builtins.path { inherit path; filter = path: _: baseNameOf path != "test.sh"; };
Expand Down

0 comments on commit 4f42628

Please sign in to comment.