1
0
Fork 0
mirror of https://github.com/hercules-ci/flake-parts.git synced 2024-12-14 11:47:31 +00:00
flake-parts/modules/perSystem.nix

116 lines
3.7 KiB
Nix
Raw Normal View History

2022-05-25 14:36:33 +00:00
{ config, lib, flake-parts-lib, self, ... }:
2021-10-27 09:05:52 +00:00
let
inherit (lib)
genAttrs
2021-10-27 09:05:52 +00:00
mapAttrs
mkOption
types
;
2022-05-25 14:36:33 +00:00
inherit (flake-parts-lib)
2022-05-13 08:14:10 +00:00
mkPerSystemType
;
2021-10-27 09:05:52 +00:00
rootConfig = config;
# Stubs for self and inputs. While it'd be possible to define aliases
# inside perSystem, that is not a general solution, and it would make
# top.config harder to discover, stretching the learning curve rather
# than flattening it.
throwAliasError' = param:
throw ''
`${param}` (without `'`) is not a `perSystem` module argument, but a
module argument of the top level config.
The following is an example usage of `${param}`. Note that its binding
is in the `top` parameter list, which is declared by the top level module
rather than the `perSystem` module.
top@{ config, lib, ${param}, ... }: {
perSystem = { config, ${param}', ... }: {
# in scope here:
# - ${param}
# - ${param}'
# - config (of perSystem)
# - top.config (note the `top@` pattern)
};
}
'';
throwAliasError = param:
throw ''
`${param}` is not a `perSystem` module argument, but a module argument of
the top level config.
The following is an example usage of `${param}`. Note that its binding
is in the `top` parameter list, which is declared by the top level module
rather than the `perSystem` module.
top@{ config, lib, ${param}, ... }: {
perSystem = { config, ... }: {
# in scope here:
# - ${param}
# - config (of perSystem)
# - top.config (note the `top@` pattern)
};
}
'';
2021-10-27 09:05:52 +00:00
in
{
options = {
systems = mkOption {
2022-11-11 06:39:25 +00:00
description = ''
2022-11-11 05:40:37 +00:00
All the system types to enumerate in the flake output subattributes.
In other words, all valid values for `system` in e.g. `packages.<system>.foo`.
'';
2021-10-27 09:05:52 +00:00
type = types.listOf types.str;
};
perInput = mkOption {
2022-11-11 06:39:25 +00:00
description = "Function from system to function from flake to `system`-specific attributes.";
2021-10-27 09:05:52 +00:00
type = types.functionTo (types.functionTo (types.lazyAttrsOf types.unspecified));
};
perSystem = mkOption {
2022-11-11 06:39:25 +00:00
description = "A function from system to flake-like attributes omitting the `<system>` attribute.";
2022-05-13 08:14:10 +00:00
type = mkPerSystemType ({ config, system, ... }: {
_file = ./perSystem.nix;
config = {
_module.args.inputs' = mapAttrs (k: rootConfig.perInput system) self.inputs;
_module.args.self' = rootConfig.perInput system self;
# Custom error messages
_module.args.self = throwAliasError' "self";
_module.args.inputs = throwAliasError' "inputs";
_module.args.getSystem = throwAliasError "getSystem";
_module.args.withSystem = throwAliasError "withSystem";
_module.args.moduleWithSystem = throwAliasError "moduleWithSystem";
2022-05-13 08:14:10 +00:00
};
2021-10-27 09:05:52 +00:00
});
apply = modules: system:
(lib.evalModules {
inherit modules;
prefix = [ "perSystem" system ];
specialArgs = {
inherit system;
};
}).config;
2021-10-27 09:05:52 +00:00
};
allSystems = mkOption {
type = types.lazyAttrsOf types.unspecified;
2022-11-11 06:39:25 +00:00
description = "The system-specific config for each of systems.";
internal = true;
};
};
config = {
allSystems = genAttrs config.systems config.perSystem;
# TODO: Sub-optimal error message. Get Nix to support a memoization primop, or get Nix Flakes to support systems properly or get Nix Flakes to add a name to flakes.
2022-04-06 16:01:26 +00:00
_module.args.getSystem = system: config.allSystems.${system} or (builtins.trace "using non-memoized system ${system}" config.perSystem system);
2021-10-27 09:05:52 +00:00
};
}