mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-06-12 04:35:41 +03:00
treewide: Format all Nix files
Format all Nix files using the officially approved formatter,
making the CI check introduced in the previous commit succeed:
nix-build ci -A fmt.check
This is the next step of the of the [implementation](https://github.com/NixOS/nixfmt/issues/153)
of the accepted [RFC 166](https://github.com/NixOS/rfcs/pull/166).
This commit will lead to merge conflicts for a number of PRs,
up to an estimated ~1100 (~33%) among the PRs with activity in the past 2
months, but that should be lower than what it would be without the previous
[partial treewide format](https://github.com/NixOS/nixpkgs/pull/322537).
Merge conflicts caused by this commit can now automatically be resolved while rebasing using the
[auto-rebase script](8616af08d9/maintainers/scripts/auto-rebase
).
If you run into any problems regarding any of this, please reach out to the
[formatting team](https://nixos.org/community/teams/formatting/) by
pinging @NixOS/nix-formatting.
This commit is contained in:
parent
2140bf39e4
commit
374e6bcc40
1523 changed files with 986047 additions and 513621 deletions
269
lib/trivial.nix
269
lib/trivial.nix
|
@ -11,11 +11,13 @@ let
|
|||
toBaseDigits
|
||||
version
|
||||
versionSuffix
|
||||
warn;
|
||||
warn
|
||||
;
|
||||
inherit (lib)
|
||||
isString
|
||||
;
|
||||
in {
|
||||
in
|
||||
{
|
||||
|
||||
## Simple (higher order) functions
|
||||
|
||||
|
@ -23,7 +25,6 @@ in {
|
|||
The identity function
|
||||
For when you need a function that does “nothing”.
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`x`
|
||||
|
@ -44,7 +45,6 @@ in {
|
|||
Ignores the second argument. If called with only one argument,
|
||||
constructs a function that always returns a static value.
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`x`
|
||||
|
@ -72,9 +72,7 @@ in {
|
|||
|
||||
:::
|
||||
*/
|
||||
const =
|
||||
x:
|
||||
y: x;
|
||||
const = x: y: x;
|
||||
|
||||
/**
|
||||
Pipes a value through a list of functions, left to right.
|
||||
|
@ -140,7 +138,6 @@ in {
|
|||
/**
|
||||
Concatenate two lists
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`x`
|
||||
|
@ -173,7 +170,6 @@ in {
|
|||
/**
|
||||
boolean “or”
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`x`
|
||||
|
@ -189,7 +185,6 @@ in {
|
|||
/**
|
||||
boolean “and”
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`x`
|
||||
|
@ -205,7 +200,6 @@ in {
|
|||
/**
|
||||
boolean “exclusive or”
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`x`
|
||||
|
@ -232,7 +226,6 @@ in {
|
|||
boolean values. Calling `toString` on a bool instead returns "1"
|
||||
and "" (sic!).
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`b`
|
||||
|
@ -252,7 +245,6 @@ in {
|
|||
|
||||
mergeAttrs :: attrs -> attrs -> attrs
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`x`
|
||||
|
@ -263,7 +255,6 @@ in {
|
|||
|
||||
: Right attribute set (higher precedence for equal keys)
|
||||
|
||||
|
||||
# Examples
|
||||
:::{.example}
|
||||
## `lib.trivial.mergeAttrs` usage example
|
||||
|
@ -275,14 +266,11 @@ in {
|
|||
|
||||
:::
|
||||
*/
|
||||
mergeAttrs =
|
||||
x:
|
||||
y: x // y;
|
||||
mergeAttrs = x: y: x // y;
|
||||
|
||||
/**
|
||||
Flip the order of the arguments of a binary function.
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`f`
|
||||
|
@ -314,12 +302,13 @@ in {
|
|||
|
||||
:::
|
||||
*/
|
||||
flip = f: a: b: f b a;
|
||||
flip =
|
||||
f: a: b:
|
||||
f b a;
|
||||
|
||||
/**
|
||||
Return `maybeValue` if not null, otherwise return `default`.
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`default`
|
||||
|
@ -330,7 +319,6 @@ in {
|
|||
|
||||
: 2\. Function argument
|
||||
|
||||
|
||||
# Examples
|
||||
:::{.example}
|
||||
## `lib.trivial.defaultTo` usage example
|
||||
|
@ -346,14 +334,11 @@ in {
|
|||
|
||||
:::
|
||||
*/
|
||||
defaultTo = default: maybeValue:
|
||||
if maybeValue != null then maybeValue
|
||||
else default;
|
||||
defaultTo = default: maybeValue: if maybeValue != null then maybeValue else default;
|
||||
|
||||
/**
|
||||
Apply function if the supplied argument is non-null.
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`f`
|
||||
|
@ -364,7 +349,6 @@ in {
|
|||
|
||||
: Argument to check for null before passing it to `f`
|
||||
|
||||
|
||||
# Examples
|
||||
:::{.example}
|
||||
## `lib.trivial.mapNullable` usage example
|
||||
|
@ -378,16 +362,25 @@ in {
|
|||
|
||||
:::
|
||||
*/
|
||||
mapNullable =
|
||||
f:
|
||||
a: if a == null then a else f a;
|
||||
mapNullable = f: a: if a == null then a else f a;
|
||||
|
||||
# Pull in some builtins not included elsewhere.
|
||||
inherit (builtins)
|
||||
pathExists readFile isBool
|
||||
isInt isFloat add sub lessThan
|
||||
seq deepSeq genericClosure
|
||||
bitAnd bitOr bitXor;
|
||||
pathExists
|
||||
readFile
|
||||
isBool
|
||||
isInt
|
||||
isFloat
|
||||
add
|
||||
sub
|
||||
lessThan
|
||||
seq
|
||||
deepSeq
|
||||
genericClosure
|
||||
bitAnd
|
||||
bitOr
|
||||
bitXor
|
||||
;
|
||||
|
||||
## nixpkgs version strings
|
||||
|
||||
|
@ -422,7 +415,6 @@ in {
|
|||
Whether a feature is supported in all supported releases (at the time of
|
||||
release branch-off, if applicable). See `oldestSupportedRelease`.
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`release`
|
||||
|
@ -433,15 +425,13 @@ in {
|
|||
isInOldestRelease =
|
||||
lib.warnIf (lib.oldestSupportedReleaseIsAtLeast 2411)
|
||||
"lib.isInOldestRelease is deprecated. Use lib.oldestSupportedReleaseIsAtLeast instead."
|
||||
lib.oldestSupportedReleaseIsAtLeast;
|
||||
lib.oldestSupportedReleaseIsAtLeast;
|
||||
|
||||
/**
|
||||
Alias for `isInOldestRelease` introduced in 24.11.
|
||||
Use `isInOldestRelease` in expressions outside of Nixpkgs for greater compatibility.
|
||||
*/
|
||||
oldestSupportedReleaseIsAtLeast =
|
||||
release:
|
||||
release <= lib.trivial.oldestSupportedRelease;
|
||||
*/
|
||||
oldestSupportedReleaseIsAtLeast = release: release <= lib.trivial.oldestSupportedRelease;
|
||||
|
||||
/**
|
||||
Returns the current nixpkgs release code name.
|
||||
|
@ -455,16 +445,15 @@ in {
|
|||
Returns the current nixpkgs version suffix as string.
|
||||
*/
|
||||
versionSuffix =
|
||||
let suffixFile = ../.version-suffix;
|
||||
in if pathExists suffixFile
|
||||
then lib.strings.fileContents suffixFile
|
||||
else "pre-git";
|
||||
let
|
||||
suffixFile = ../.version-suffix;
|
||||
in
|
||||
if pathExists suffixFile then lib.strings.fileContents suffixFile else "pre-git";
|
||||
|
||||
/**
|
||||
Attempts to return the the current revision of nixpkgs and
|
||||
returns the supplied default value otherwise.
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`default`
|
||||
|
@ -481,11 +470,14 @@ in {
|
|||
default:
|
||||
let
|
||||
revisionFile = "${toString ./..}/.git-revision";
|
||||
gitRepo = "${toString ./..}/.git";
|
||||
in if lib.pathIsGitRepo gitRepo
|
||||
then lib.commitIdFromGitRepo gitRepo
|
||||
else if lib.pathExists revisionFile then lib.fileContents revisionFile
|
||||
else default;
|
||||
gitRepo = "${toString ./..}/.git";
|
||||
in
|
||||
if lib.pathIsGitRepo gitRepo then
|
||||
lib.commitIdFromGitRepo gitRepo
|
||||
else if lib.pathExists revisionFile then
|
||||
lib.fileContents revisionFile
|
||||
else
|
||||
default;
|
||||
|
||||
nixpkgsVersion = warn "lib.nixpkgsVersion is a deprecated alias of lib.version." version;
|
||||
|
||||
|
@ -512,14 +504,13 @@ in {
|
|||
inPureEvalMode :: bool
|
||||
```
|
||||
*/
|
||||
inPureEvalMode = ! builtins ? currentSystem;
|
||||
inPureEvalMode = !builtins ? currentSystem;
|
||||
|
||||
## Integer operations
|
||||
|
||||
/**
|
||||
Return minimum of two numbers.
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`x`
|
||||
|
@ -535,7 +526,6 @@ in {
|
|||
/**
|
||||
Return maximum of two numbers.
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`x`
|
||||
|
@ -551,7 +541,6 @@ in {
|
|||
/**
|
||||
Integer modulus
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`base`
|
||||
|
@ -562,7 +551,6 @@ in {
|
|||
|
||||
: 2\. Function argument
|
||||
|
||||
|
||||
# Examples
|
||||
:::{.example}
|
||||
## `lib.trivial.mod` usage example
|
||||
|
@ -578,7 +566,6 @@ in {
|
|||
*/
|
||||
mod = base: int: base - (int * (builtins.div base int));
|
||||
|
||||
|
||||
## Comparisons
|
||||
|
||||
/**
|
||||
|
@ -588,7 +575,6 @@ in {
|
|||
a == b, compare a b => 0
|
||||
a > b, compare a b => 1
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`a`
|
||||
|
@ -599,12 +585,14 @@ in {
|
|||
|
||||
: 2\. Function argument
|
||||
*/
|
||||
compare = a: b:
|
||||
if a < b
|
||||
then -1
|
||||
else if a > b
|
||||
then 1
|
||||
else 0;
|
||||
compare =
|
||||
a: b:
|
||||
if a < b then
|
||||
-1
|
||||
else if a > b then
|
||||
1
|
||||
else
|
||||
0;
|
||||
|
||||
/**
|
||||
Split type into two subtypes by predicate `p`, take all elements
|
||||
|
@ -612,7 +600,6 @@ in {
|
|||
second subtype, compare elements of a single subtype with `yes`
|
||||
and `no` respectively.
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`p`
|
||||
|
@ -661,10 +648,12 @@ in {
|
|||
*/
|
||||
splitByAndCompare =
|
||||
p: yes: no: a: b:
|
||||
if p a
|
||||
then if p b then yes a b else -1
|
||||
else if p b then 1 else no a b;
|
||||
|
||||
if p a then
|
||||
if p b then yes a b else -1
|
||||
else if p b then
|
||||
1
|
||||
else
|
||||
no a b;
|
||||
|
||||
/**
|
||||
Reads a JSON file.
|
||||
|
@ -713,8 +702,7 @@ in {
|
|||
importJSON :: path -> any
|
||||
```
|
||||
*/
|
||||
importJSON = path:
|
||||
builtins.fromJSON (builtins.readFile path);
|
||||
importJSON = path: builtins.fromJSON (builtins.readFile path);
|
||||
|
||||
/**
|
||||
Reads a TOML file.
|
||||
|
@ -761,11 +749,9 @@ in {
|
|||
importTOML :: path -> any
|
||||
```
|
||||
*/
|
||||
importTOML = path:
|
||||
builtins.fromTOML (builtins.readFile path);
|
||||
importTOML = path: builtins.fromTOML (builtins.readFile path);
|
||||
|
||||
/**
|
||||
|
||||
`warn` *`message`* *`value`*
|
||||
|
||||
Print a warning before returning the second argument.
|
||||
|
@ -792,19 +778,26 @@ in {
|
|||
warn =
|
||||
# Since Nix 2.23, https://github.com/NixOS/nix/pull/10592
|
||||
builtins.warn or (
|
||||
let mustAbort = lib.elem (builtins.getEnv "NIX_ABORT_ON_WARN") ["1" "true" "yes"];
|
||||
let
|
||||
mustAbort = lib.elem (builtins.getEnv "NIX_ABORT_ON_WARN") [
|
||||
"1"
|
||||
"true"
|
||||
"yes"
|
||||
];
|
||||
in
|
||||
# Do not eta reduce v, so that we have the same strictness as `builtins.warn`.
|
||||
msg: v:
|
||||
# `builtins.warn` requires a string message, so we enforce that in our implementation, so that callers aren't accidentally incompatible with newer Nix versions.
|
||||
assert isString msg;
|
||||
if mustAbort
|
||||
then builtins.trace "[1;31mevaluation warning:[0m ${msg}" (abort "NIX_ABORT_ON_WARN=true; warnings are treated as unrecoverable errors.")
|
||||
else builtins.trace "[1;35mevaluation warning:[0m ${msg}" v
|
||||
# Do not eta reduce v, so that we have the same strictness as `builtins.warn`.
|
||||
msg: v:
|
||||
# `builtins.warn` requires a string message, so we enforce that in our implementation, so that callers aren't accidentally incompatible with newer Nix versions.
|
||||
assert isString msg;
|
||||
if mustAbort then
|
||||
builtins.trace "[1;31mevaluation warning:[0m ${msg}" (
|
||||
abort "NIX_ABORT_ON_WARN=true; warnings are treated as unrecoverable errors."
|
||||
)
|
||||
else
|
||||
builtins.trace "[1;35mevaluation warning:[0m ${msg}" v
|
||||
);
|
||||
|
||||
/**
|
||||
|
||||
`warnIf` *`condition`* *`message`* *`value`*
|
||||
|
||||
Like `warn`, but only warn when the first argument is `true`.
|
||||
|
@ -832,7 +825,6 @@ in {
|
|||
warnIf = cond: msg: if cond then warn msg else x: x;
|
||||
|
||||
/**
|
||||
|
||||
`warnIfNot` *`condition`* *`message`* *`value`*
|
||||
|
||||
Like `warnIf`, but negated: warn if the first argument is `false`.
|
||||
|
@ -870,7 +862,6 @@ in {
|
|||
Calls can be juxtaposed using function application, as `(r: r) a = a`, so
|
||||
`(r: r) (r: r) a = a`, and so forth.
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`cond`
|
||||
|
@ -904,7 +895,6 @@ in {
|
|||
/**
|
||||
Like throwIfNot, but negated (throw if the first argument is `true`).
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`cond`
|
||||
|
@ -926,7 +916,6 @@ in {
|
|||
/**
|
||||
Check if the elements in a list are valid values from a enum, returning the identity function, or throwing an error message otherwise.
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`msg`
|
||||
|
@ -960,12 +949,13 @@ in {
|
|||
|
||||
:::
|
||||
*/
|
||||
checkListOfEnum = msg: valid: given:
|
||||
checkListOfEnum =
|
||||
msg: valid: given:
|
||||
let
|
||||
unexpected = lib.subtractLists valid given;
|
||||
in
|
||||
lib.throwIfNot (unexpected == [])
|
||||
"${msg}: ${builtins.concatStringsSep ", " (builtins.map builtins.toString unexpected)} unexpected; valid ones: ${builtins.concatStringsSep ", " (builtins.map builtins.toString valid)}";
|
||||
lib.throwIfNot (unexpected == [ ])
|
||||
"${msg}: ${builtins.concatStringsSep ", " (builtins.map builtins.toString unexpected)} unexpected; valid ones: ${builtins.concatStringsSep ", " (builtins.map builtins.toString valid)}";
|
||||
|
||||
info = msg: builtins.trace "INFO: ${msg}";
|
||||
|
||||
|
@ -984,7 +974,6 @@ in {
|
|||
function of the { a, b ? foo, ... }: format, but some facilities
|
||||
like callPackage expect to be able to query expected arguments.
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`f`
|
||||
|
@ -995,11 +984,11 @@ in {
|
|||
|
||||
: 2\. Function argument
|
||||
*/
|
||||
setFunctionArgs = f: args:
|
||||
{ # TODO: Should we add call-time "type" checking like built in?
|
||||
__functor = self: f;
|
||||
__functionArgs = args;
|
||||
};
|
||||
setFunctionArgs = f: args: {
|
||||
# TODO: Should we add call-time "type" checking like built in?
|
||||
__functor = self: f;
|
||||
__functionArgs = args;
|
||||
};
|
||||
|
||||
/**
|
||||
Extract the expected function arguments from a function.
|
||||
|
@ -1008,37 +997,35 @@ in {
|
|||
has the same return type and semantics as builtins.functionArgs.
|
||||
setFunctionArgs : (a → b) → Map String Bool.
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`f`
|
||||
|
||||
: 1\. Function argument
|
||||
*/
|
||||
functionArgs = f:
|
||||
if f ? __functor
|
||||
then f.__functionArgs or (functionArgs (f.__functor f))
|
||||
else builtins.functionArgs f;
|
||||
functionArgs =
|
||||
f:
|
||||
if f ? __functor then
|
||||
f.__functionArgs or (functionArgs (f.__functor f))
|
||||
else
|
||||
builtins.functionArgs f;
|
||||
|
||||
/**
|
||||
Check whether something is a function or something
|
||||
annotated with function args.
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`f`
|
||||
|
||||
: 1\. Function argument
|
||||
*/
|
||||
isFunction = f: builtins.isFunction f ||
|
||||
(f ? __functor && isFunction (f.__functor f));
|
||||
isFunction = f: builtins.isFunction f || (f ? __functor && isFunction (f.__functor f));
|
||||
|
||||
/**
|
||||
`mirrorFunctionArgs f g` creates a new function `g'` with the same behavior as `g` (`g' x == g x`)
|
||||
but its function arguments mirroring `f` (`lib.functionArgs g' == lib.functionArgs f`).
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`f`
|
||||
|
@ -1084,21 +1071,18 @@ in {
|
|||
let
|
||||
fArgs = functionArgs f;
|
||||
in
|
||||
g:
|
||||
setFunctionArgs g fArgs;
|
||||
g: setFunctionArgs g fArgs;
|
||||
|
||||
/**
|
||||
Turns any non-callable values into constant functions.
|
||||
Returns callable values as is.
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`v`
|
||||
|
||||
: Any value
|
||||
|
||||
|
||||
# Examples
|
||||
:::{.example}
|
||||
## `lib.trivial.toFunction` usage example
|
||||
|
@ -1113,11 +1097,7 @@ in {
|
|||
|
||||
:::
|
||||
*/
|
||||
toFunction =
|
||||
v:
|
||||
if isFunction v
|
||||
then v
|
||||
else k: v;
|
||||
toFunction = v: if isFunction v then v else k: v;
|
||||
|
||||
/**
|
||||
Convert a hexadecimal string to it's integer representation.
|
||||
|
@ -1138,12 +1118,15 @@ in {
|
|||
=> 9223372036854775807
|
||||
```
|
||||
*/
|
||||
fromHexString = value:
|
||||
let
|
||||
noPrefix = lib.strings.removePrefix "0x" (lib.strings.toLower value);
|
||||
in let
|
||||
parsed = builtins.fromTOML "v=0x${noPrefix}";
|
||||
in parsed.v;
|
||||
fromHexString =
|
||||
value:
|
||||
let
|
||||
noPrefix = lib.strings.removePrefix "0x" (lib.strings.toLower value);
|
||||
in
|
||||
let
|
||||
parsed = builtins.fromTOML "v=0x${noPrefix}";
|
||||
in
|
||||
parsed.v;
|
||||
|
||||
/**
|
||||
Convert the given positive integer to a string of its hexadecimal
|
||||
|
@ -1155,20 +1138,19 @@ in {
|
|||
|
||||
toHexString 250 => "FA"
|
||||
*/
|
||||
toHexString = let
|
||||
hexDigits = {
|
||||
"10" = "A";
|
||||
"11" = "B";
|
||||
"12" = "C";
|
||||
"13" = "D";
|
||||
"14" = "E";
|
||||
"15" = "F";
|
||||
};
|
||||
toHexDigit = d:
|
||||
if d < 10
|
||||
then toString d
|
||||
else hexDigits.${toString d};
|
||||
in i: lib.concatMapStrings toHexDigit (toBaseDigits 16 i);
|
||||
toHexString =
|
||||
let
|
||||
hexDigits = {
|
||||
"10" = "A";
|
||||
"11" = "B";
|
||||
"12" = "C";
|
||||
"13" = "D";
|
||||
"14" = "E";
|
||||
"15" = "F";
|
||||
};
|
||||
toHexDigit = d: if d < 10 then toString d else hexDigits.${toString d};
|
||||
in
|
||||
i: lib.concatMapStrings toHexDigit (toBaseDigits 16 i);
|
||||
|
||||
/**
|
||||
`toBaseDigits base i` converts the positive integer i to a list of its
|
||||
|
@ -1180,7 +1162,6 @@ in {
|
|||
|
||||
toBaseDigits 16 250 => [ 15 10 ]
|
||||
|
||||
|
||||
# Inputs
|
||||
|
||||
`base`
|
||||
|
@ -1191,21 +1172,23 @@ in {
|
|||
|
||||
: 2\. Function argument
|
||||
*/
|
||||
toBaseDigits = base: i:
|
||||
toBaseDigits =
|
||||
base: i:
|
||||
let
|
||||
go = i:
|
||||
if i < base
|
||||
then [i]
|
||||
go =
|
||||
i:
|
||||
if i < base then
|
||||
[ i ]
|
||||
else
|
||||
let
|
||||
r = i - ((i / base) * base);
|
||||
q = (i - r) / base;
|
||||
in
|
||||
[r] ++ go q;
|
||||
[ r ] ++ go q;
|
||||
in
|
||||
assert (isInt base);
|
||||
assert (isInt i);
|
||||
assert (base >= 2);
|
||||
assert (i >= 0);
|
||||
lib.reverseList (go i);
|
||||
assert (isInt base);
|
||||
assert (isInt i);
|
||||
assert (base >= 2);
|
||||
assert (i >= 0);
|
||||
lib.reverseList (go i);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue