Skip to content

Commit

Permalink
Apply nixfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
oxalica committed Sep 19, 2024
1 parent 9125bfe commit c8e8ce7
Show file tree
Hide file tree
Showing 8 changed files with 361 additions and 259 deletions.
304 changes: 176 additions & 128 deletions crates/nix-interop/src/nixos_options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,48 @@ let
modulePath = nixpkgs + "/nixos/modules";
moduleListPath = modulePath + "/module-list.nix";

inherit (builtins) isString filter mapAttrs isPath isFunction functionArgs pathExists;
inherit (lib) evalModules trivial optionals filterAttrs;
inherit (builtins)
isString
filter
mapAttrs
isPath
isFunction
functionArgs
pathExists
;
inherit (lib)
evalModules
trivial
optionals
filterAttrs
;
inherit (lib.options) unknownModule literalExpression;

# Polyfill for < 23.05
renderOptionValue = lib.options.renderOptionValue or (v:
if v ? _type && v ? text then v
else literalExpression (lib.generators.toPretty {
multiline = true;
allowPrettyValues = true;
} v));
renderOptionValue =
lib.options.renderOptionValue or (
v:
if v ? _type && v ? text then
v
else
literalExpression (
lib.generators.toPretty {
multiline = true;
allowPrettyValues = true;
} v
)
);

# Special tranfrom for < 23.05
renderDoc = v:
if isString v
then { _type = "mdDoc"; text = v; }
else v;
renderDoc =
v:
if isString v then
{
_type = "mdDoc";
text = v;
}
else
v;

# Dummy `pkgs`.
pkgs = import (nixpkgs + "/pkgs/pkgs-lib") {
Expand All @@ -39,19 +64,17 @@ let
modules = filter canCacheDocs (import moduleListPath);

# From `nixos/modules/misc/documentation.nix`.
canCacheDocs = m:
canCacheDocs =
m:
let
f = import m;
instance = f (mapAttrs (n: _: abort "evaluating ${n} for `meta` failed") (functionArgs f));
in
isPath m
&& isFunction f
&& instance ? options
&& instance.meta.buildDocsInSandbox or true;
isPath m && isFunction f && instance ? options && instance.meta.buildDocsInSandbox or true;

config = {
_module.check = false;
_module.args = {};
_module.args = { };
system.stateVersion = trivial.release;
};
eval = evalModules {
Expand All @@ -62,122 +85,147 @@ let
};

# https://github.com/NixOS/nixpkgs/blob/28c1aac72e3aef70b8c898ea9c16d5907f9eae22/lib/types.nix#L212
normalizeType = submoduleVisible: ty: let
elem = normalizeType submoduleVisible ty.nestedTypes.elemType;
ty' = rec {
anything.name = "any";
raw = anything;
unspecified = anything;

bool.name = "bool";

int.name = "int";
intBetween = int;
unsignedInt = int;
positiveInt = int;
signedInt8 = int;
signedInt16 = int;
signedInt32 = int;
unsignedInt8 = int;
unsignedInt16 = int;
unsignedInt32 = int;

float.name = "float";
number = float;
numberBetween = float;
numberNonnegative = float;
numberPositive = float;

str.name = "string";
nonEmptyStr = str;
singleLineStr = str;
# strMatching<regex>
separatedString = str;
string = str;
# passwdEntry<name>

attrs = { name = "attrset"; rest = anything; };
package.name = "derivation";
shellPackage.name = "derivation";

path.name = "path";

listOf = { name = "list"; inherit elem; };

attrsOf = { name = "attrset"; rest = elem; };
lazyAttrsOf = { name = "attrset"; rest = elem; };

uniq = elem;
unique = elem;

# FIXME: Union and null type.
nullOr = elem;

functionTo = { name = "lambda"; from = anything; to = elem; };

submodule = if submoduleVisible then {
name = "attrset";
fields = normalizeOptionSet (ty.getSubOptions [ ]);
} else {
name = "any";
};
deferredModule = submodule;

optionType = { name = "attrset"; rest = anything; };

# enum
# either
# oneOf
# coerceTo
}.${ty.name} or { name = "any"; };
in
normalizeType =
submoduleVisible: ty:
let
elem = normalizeType submoduleVisible ty.nestedTypes.elemType;
ty' =
rec {
anything.name = "any";
raw = anything;
unspecified = anything;

bool.name = "bool";

int.name = "int";
intBetween = int;
unsignedInt = int;
positiveInt = int;
signedInt8 = int;
signedInt16 = int;
signedInt32 = int;
unsignedInt8 = int;
unsignedInt16 = int;
unsignedInt32 = int;

float.name = "float";
number = float;
numberBetween = float;
numberNonnegative = float;
numberPositive = float;

str.name = "string";
nonEmptyStr = str;
singleLineStr = str;
# strMatching<regex>
separatedString = str;
string = str;
# passwdEntry<name>

attrs = {
name = "attrset";
rest = anything;
};
package.name = "derivation";
shellPackage.name = "derivation";

path.name = "path";

listOf = {
name = "list";
inherit elem;
};

attrsOf = {
name = "attrset";
rest = elem;
};
lazyAttrsOf = {
name = "attrset";
rest = elem;
};

uniq = elem;
unique = elem;

# FIXME: Union and null type.
nullOr = elem;

functionTo = {
name = "lambda";
from = anything;
to = elem;
};

submodule =
if submoduleVisible then
{
name = "attrset";
fields = normalizeOptionSet (ty.getSubOptions [ ]);
}
else
{
name = "any";
};
deferredModule = submodule;

optionType = {
name = "attrset";
rest = anything;
};

# enum
# either
# oneOf
# coerceTo
}
.${ty.name} or {
name = "any";
};
in
assert ty._type or null == "option-type";
ty';

# Modified from `lib.optionAttrSetToDocList`.
normalizeOptions = opt: let
# visible: true | false | "shallow"
visible = (opt.visible or true != false) && !(opt.internal or false);
submoduleVisible = visible && (opt.visible or true == true);

opt' = {
description = renderDoc (opt.description or null);
declarations = filter (x: x != unknownModule) opt.declarations;
readOnly = opt.readOnly or false;
type = normalizeType submoduleVisible opt.type;
example =
if opt ? example then
renderOptionValue opt.example
else
null;
default =
if opt ? default then
renderOptionValue (opt.defaultText or opt.default)
else
null;
relatedPackages =
optionals (opt.relatedPackages or null != null)
opt.relatedPackages;
};
in
normalizeOptions =
opt:
let
# visible: true | false | "shallow"
visible = (opt.visible or true != false) && !(opt.internal or false);
submoduleVisible = visible && (opt.visible or true == true);

opt' = {
description = renderDoc (opt.description or null);
declarations = filter (x: x != unknownModule) opt.declarations;
readOnly = opt.readOnly or false;
type = normalizeType submoduleVisible opt.type;
example = if opt ? example then renderOptionValue opt.example else null;
default = if opt ? default then renderOptionValue (opt.defaultText or opt.default) else null;
relatedPackages = optionals (opt.relatedPackages or null != null) opt.relatedPackages;
};
in
if opt._type or null == "option" then
if visible then
opt'
else
null
else {
type = {
name = "attrset";
fields = normalizeOptionSet opt;
if visible then opt' else null
else
{
type = {
name = "attrset";
fields = normalizeOptionSet opt;
};
};
};

normalizeOptionSet = opts:
filterAttrs (k: v: k != "_module" && k != "_freeformOptions" && !isNull v)
(mapAttrs (_: normalizeOptions) opts);
normalizeOptionSet =
opts:
filterAttrs (k: v: k != "_module" && k != "_freeformOptions" && !isNull v) (
mapAttrs (_: normalizeOptions) opts
);

in
if pathExists libPath && pathExists moduleListPath
&& builtins.compareVersions trivial.release "22.11" >= 0
then normalizeOptionSet eval.options
else { }
if
pathExists libPath
&& pathExists moduleListPath
&& builtins.compareVersions trivial.release "22.11" >= 0
then
normalizeOptionSet eval.options
else
{ }
11 changes: 6 additions & 5 deletions crates/nix-interop/tests/oom_flake/flake.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
outputs = { self }:
# Allocates a list with ~1G elements.
# NB. This evaluates to false, so it will never be cached.
assert builtins.length (builtins.head (builtins.genList (x: x) 1000000000)) == 42;
{ };
outputs =
{ self }:
# Allocates a list with ~1G elements.
# NB. This evaluates to false, so it will never be cached.
assert builtins.length (builtins.head (builtins.genList (x: x) 1000000000)) == 42;
{ };
}
Loading

0 comments on commit c8e8ce7

Please sign in to comment.