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

Provide flake parts module #597

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
8 changes: 7 additions & 1 deletion modules/flake-parts/all-modules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ in {
};

# generates future flake outputs: `modules.<kind>.<module-name>`
config.flake.modules = lib.mapAttrs (kind: _: mapModules kind) moduleKinds;
config.flake.modules =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've assumed that this was for internal purposes, and I think it would make sense to have something like this in flake-parts for publishing modules.

It would also be nice to have an option for local modules that aren't published.

let modules = lib.mapAttrs (kind: _: mapModules kind) moduleKinds;
in modules // {
flake-parts = modules.flake-parts // {
all-modules = { imports = flakePartsModules; _class = "flake-parts"; };
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kinds are actually called class in evalModules or _class in the module syntax.
It's a fairly new thing, not too long before 23.05. https://nixos.org/manual/nixpkgs/stable/#module-system-lib-evalModules-param-class

Haven't used class much yet as it's incompatible with 22.11, but that's increasingly no excuse anymore ;)

};
};

# comapt to current schema: `nixosModules` / `darwinModules`
config.flake.nixosModules = config.flake.modules.nixos or {};
Expand Down
25 changes: 20 additions & 5 deletions modules/flake-parts/writers.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
perSystem = {
{ flake-parts-lib, ... }: {
options.perSystem = flake-parts-lib.mkPerSystemOption ({
config,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes the options statically available for rendering.

lib,
pkgs,
Expand All @@ -9,10 +9,25 @@
in {
options.writers = {
writePureShellScript = lib.mkOption {
type = lib.types.functionTo lib.types.anything;
type = lib.types.functionTo (lib.types.functionTo lib.types.package);
description = ''
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved the documentation here, but probably the option declarations should be moved closer to the writers.

Sort-of like this:

options.writers = lib.mkOption { type = submodule ../writers.nix; }

Create a script that runs in a `pure` environment, in the sense that:
- `PATH` only contains exactly the packages passed via the `PATH` arg
- `NIX_PATH` is set to the path of the current `pkgs`
- `TMPDIR` is set up and cleaned up even if the script fails
- out, if set, is kept as-is
- all environment variables are unset, except:
- the ones listed in `keepVars` below
- ones listed via the `KEEP_VARS` variable
- the behavior is similar to `nix-shell --pure`
'';
};
writePureShellScriptBin = lib.mkOption {
type = lib.types.functionTo lib.types.anything;
type = lib.types.functionTo (lib.types.functionTo (lib.types.functionTo lib.types.package));
description = ''
Creates a script in a `bin/` directory in the output; suitable for use with `lib.makeBinPath`, etc.
See {option}`writers.writePureShellScript`
'';
};
};

Expand All @@ -23,5 +38,5 @@
writePureShellScriptBin
;
};
};
});
}
13 changes: 2 additions & 11 deletions pkgs/writers/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,11 @@
writeScriptBin,
...
}: let
/*
create a script that runs in a `pure` environment, in the sense that:
- PATH only contains exactly the packages passed via the PATH arg
- NIX_PATH is set to the path of the current `pkgs`
- TMPDIR is set up and cleaned up even if the script fails
- out, if set, is kept as-is
- all environment variables are unset, except:
- the ones listed in `keepVars` below
- ones listed via the KEEP_VARS variable
- the behavior is similar to `nix-shell --pure`
*/
# Docs at modules/flake-parts/writers.nix
writePureShellScript = PATH: script:
writeScript "script.sh" (mkScript PATH script);

# Docs at modules/flake-parts/writers.nix
writePureShellScriptBin = binName: PATH: script:
writeScriptBin binName (mkScript PATH script);

Expand Down
Loading