Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lib.mkFlake #86

Merged
merged 4 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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