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

lib.types: Add flake type #210572

Draft
wants to merge 1 commit into
base: master
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
17 changes: 17 additions & 0 deletions lib/tests/modules.sh
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,23 @@ checkConfigOutput '^42$' config.value ./declare-oneOf.nix ./define-value-int-pos
checkConfigOutput '^\[ \]$' config.value ./declare-oneOf.nix ./define-value-list.nix
checkConfigOutput '^"24"$' config.value ./declare-oneOf.nix ./define-value-string.nix

test_flake_type() {
# A flake must be returned without any pre or post-processing.
checkConfigOutput "^true$" config.result ./declare-flake.nix ./define-flake.nix

# Even an identical flake can not be merged. In the "best" case it's the same flake,
# but we'd have to check that is. Perhaps some attributes could be merged, but
# many can't, so this would effectively create two _usage_ types:
# - full access, allowing access to `sourceInfo` and `packages.default`
# - only access to attributes that can be expected to be merged, such as
# `packages.foo`
# However, the latter would still break the rule that an addition somewhere "unrelated"
# doesn't break anything. So merging would be a bad idea.
checkConfigError "The option .flake. is defined multiple times" config.result ./declare-flake.nix ./define-flake.nix ./define-flake-again.nix
checkConfigError "However, flakes can not be merged" config.result ./declare-flake.nix ./define-flake.nix ./define-flake-again.nix
}
test_flake_type

# Check mkForce without submodules.
set -- config.enable ./declare-enable.nix ./define-enable.nix
checkConfigOutput '^true$' "$@"
Expand Down
3 changes: 3 additions & 0 deletions lib/tests/modules/declare-flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{ lib, ... }: {
options.flake = lib.mkOption { type = lib.types.flake; };
}
15 changes: 15 additions & 0 deletions lib/tests/modules/define-flake-again.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{ config, lib, ... }:
let example = {
_type = "flake";
sourceInfo = {};
inputs = {};
outputs = {};
foo = lib.mkForce {}; # must be copied verbatim
packages = throw "Validating the flake is not the responsibility of this type, and evaluating any significant part of it would be too costly.";
};
in
{
config = {
flake = example;
};
}
17 changes: 17 additions & 0 deletions lib/tests/modules/define-flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{ config, lib, ... }:
let example = {
_type = "flake";
sourceInfo = {};
inputs = {};
outputs = {};
foo = lib.mkForce {}; # must be copied verbatim
packages = throw "Validating the flake is not the responsibility of this type, and evaluating any significant part of it would be too costly.";
};
in
{
options.result = lib.mkOption {};
config = {
flake = example;
result = config.flake // { packages = null; } == example // { packages = null; };
};
}
12 changes: 12 additions & 0 deletions lib/types.nix
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,18 @@ rec {
merge = mergeEqualOption;
};

flake = mkOptionType {
name = "flake";
descriptionClass = "noun";
check =
if builtins.compareVersions builtins.nixVersion "2.12.0" >= 0
then
x: x._type or null == "flake"
else
x: x?inputs && x?sourceInfo && x?outputs;
merge = mergeUniqueOption { message = "However, flakes can not be merged."; };
};

listOf = elemType: mkOptionType rec {
name = "listOf";
description = "list of ${optionDescriptionPhrase (class: class == "noun" || class == "composite") elemType}";
Expand Down