Skip to content

Commit

Permalink
Merge remote-tracking branch 'other/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Sep 1, 2024
2 parents b741d90 + bfdd658 commit 7f0b9e4
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 44 deletions.
3 changes: 2 additions & 1 deletion lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ let
inherit (self.derivations) lazyDerivation optionalDrvAttr;
inherit (self.meta) addMetaAttrs dontDistribute setName updateName
appendToName mapDerivationAttrset setPrio lowPrio lowPrioSet hiPrio
hiPrioSet getLicenseFromSpdxId getLicenseFromSpdxIdOr getExe getExe';
hiPrioSet licensesSpdx getLicenseFromSpdxId getLicenseFromSpdxIdOr
getExe getExe';
inherit (self.filesystem) pathType pathIsDirectory pathIsRegularFile
packagesFromDirectoryRecursive;
inherit (self.sources) cleanSourceFilter
Expand Down
6 changes: 6 additions & 0 deletions lib/licenses.nix
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,12 @@ lib.mapAttrs mkLicense ({
fullName = "CeCILL-C Free Software License Agreement";
};

cockroachdb-community-license = {
fullName = "CockroachDB Community License Agreement";
url = "https://www.cockroachlabs.com/cockroachdb-community-license/";
free = false;
};

cpal10 = {
spdxId = "CPAL-1.0";
fullName = "Common Public Attribution License 1.0";
Expand Down
57 changes: 46 additions & 11 deletions lib/meta.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

let
inherit (lib) matchAttrs any all isDerivation getBin assertMsg;
inherit (lib.attrsets) mapAttrs' filterAttrs;
inherit (builtins) isString match typeOf;

in
Expand Down Expand Up @@ -132,12 +133,17 @@ rec {
mapDerivationAttrset = f: set: lib.mapAttrs (name: pkg: if lib.isDerivation pkg then (f pkg) else pkg) set;

/**
Set the nix-env priority of the package.
The default priority of packages in Nix. See `defaultPriority` in [`src/nix/profile.cc`](https://github.com/NixOS/nix/blob/master/src/nix/profile.cc#L47).
*/
defaultPriority = 5;

/**
Set the nix-env priority of the package. Note that higher values are lower priority, and vice versa.
# Inputs
`priority`
: 1\. Function argument
: 1\. The priority to set.
`drv`
: 2\. Function argument
Expand All @@ -158,8 +164,7 @@ rec {
lowPrio = setPrio 10;

/**
Apply lowPrio to an attrset with derivations
Apply lowPrio to an attrset with derivations.
# Inputs
Expand All @@ -183,8 +188,7 @@ rec {
hiPrio = setPrio (-10);

/**
Apply hiPrio to an attrset with derivations
Apply hiPrio to an attrset with derivations.
# Inputs
Expand Down Expand Up @@ -286,11 +290,39 @@ rec {
((!pkg?meta.platforms) || any (platformMatch platform) pkg.meta.platforms) &&
all (elem: !platformMatch platform elem) (pkg.meta.badPlatforms or []);

/**
Mapping of SPDX ID to the attributes in lib.licenses.
For SPDX IDs, see https://spdx.org/licenses.
Note that some SPDX licenses might be missing.
# Examples
:::{.example}
## `lib.meta.licensesSpdx` usage example
```nix
lib.licensesSpdx.MIT == lib.licenses.mit
=> true
lib.licensesSpdx."MY LICENSE"
=> error: attribute 'MY LICENSE' missing
```
:::
*/
licensesSpdx =
mapAttrs'
(_key: license: {
name = license.spdxId;
value = license;
})
(filterAttrs (_key: license: license ? spdxId) lib.licenses);

/**
Get the corresponding attribute in lib.licenses from the SPDX ID
or warn and fallback to `{ shortName = <license string>; }`.
For SPDX IDs, see https://spdx.org/licenses
For SPDX IDs, see https://spdx.org/licenses.
Note that some SPDX licenses might be missing.
# Type
Expand Down Expand Up @@ -325,7 +357,8 @@ rec {
Get the corresponding attribute in lib.licenses from the SPDX ID
or fallback to the given default value.
For SPDX IDs, see https://spdx.org/licenses
For SPDX IDs, see https://spdx.org/licenses.
Note that some SPDX licenses might be missing.
# Inputs
Expand Down Expand Up @@ -361,10 +394,12 @@ rec {
*/
getLicenseFromSpdxIdOr =
let
spdxLicenses = lib.mapAttrs (id: ls: assert lib.length ls == 1; builtins.head ls)
(lib.groupBy (l: lib.toLower l.spdxId) (lib.filter (l: l ? spdxId) (lib.attrValues lib.licenses)));
lowercaseLicenses = lib.mapAttrs' (name: value: {
name = lib.toLower name;
inherit value;
}) licensesSpdx;
in licstr: default:
spdxLicenses.${ lib.toLower licstr } or default;
lowercaseLicenses.${ lib.toLower licstr } or default;

/**
Get the path to the main program of a package based on meta.mainProgram
Expand Down
53 changes: 53 additions & 0 deletions lib/modules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,58 @@ let
]);
};

/**
`importApply file arg :: Path -> a -> Module`, where `import file :: a -> Module`
`importApply` imports a Nix expression file much like the module system would,
after passing an extra positional argument to the function in the file.
This function should be used when declaring a module in a file that refers to
values from a different scope, such as that in a flake.
It solves the problems of alternative solutions:
- While `importApply file arg` is _mostly_ equivalent to
`import file arg`, the latter returns a module without a location,
as `import` only returns the contained expression. This leads to worse
error messages.
- Using `specialArgs` to provide arguments to all modules. This effectively
creates an incomplete module, and requires the user of the module to
manually pass the `specialArgs` to the configuration, which is error-prone,
verbose, and unnecessary.
The nix file must contain a function that returns a module.
A module may itself be a function, so the file is often a function with two
positional arguments instead of one. See the example below.
This function does not add support for deduplication and `disabledModules`,
although that could be achieved by wrapping the returned module and setting
the `_key` module attribute.
The reason for this omission is that the file path is not guaranteed to be
a unique identifier for the module, as two instances of the module may
reference different `arg`s in their closures.
Example
# lib.nix
imports = [
(lib.modules.importApply ./module.nix { bar = bar; })
];
# module.nix
{ bar }:
{ lib, config, ... }:
{
options = ...;
config = ... bar ...;
}
*/
importApply =
modulePath: staticArg:
lib.setDefaultModuleLocation modulePath (import modulePath staticArg);

/* Use this function to import a JSON file as NixOS configuration.
modules.importJSON :: path -> attrs
Expand Down Expand Up @@ -1415,6 +1467,7 @@ private //
filterOverrides'
fixMergeModules
fixupOptionType # should be private?
importApply
importJSON
importTOML
mergeDefinitions
Expand Down
16 changes: 12 additions & 4 deletions lib/strings.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,8 @@ rec {
replaceStrings (builtins.attrNames toEscape) (lib.mapAttrsToList (_: c: "%${fixedWidthString 2 "0" (lib.toHexString c)}") toEscape);

/**
Quote `string` to be used safely within the Bourne shell.
Quote `string` to be used safely within the Bourne shell if it has any
special characters.
# Inputs
Expand All @@ -1051,10 +1052,17 @@ rec {
:::
*/
escapeShellArg = arg: "'${replaceStrings ["'"] ["'\\''"] (toString arg)}'";
escapeShellArg = arg:
let
string = toString arg;
in
if match "[[:alnum:],._+:@%/-]+" string == null
then "'${replaceStrings ["'"] ["'\\''"] string}'"
else string;

/**
Quote all arguments to be safely passed to the Bourne shell.
Quote all arguments that have special characters to be safely passed to the
Bourne shell.
# Inputs
Expand All @@ -1073,7 +1081,7 @@ rec {
```nix
escapeShellArgs ["one" "two three" "four'five"]
=> "'one' 'two three' 'four'\\''five'"
=> "one 'two three' 'four'\\''five'"
```
:::
Expand Down
3 changes: 1 addition & 2 deletions lib/systems/doubles.nix
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ in {

arm = filterDoubles predicates.isAarch32;
armv7 = filterDoubles predicates.isArmv7;
aarch = filterDoubles predicates.isAarch;
aarch64 = filterDoubles predicates.isAarch64;
x86 = filterDoubles predicates.isx86;
i686 = filterDoubles predicates.isi686;
Expand Down Expand Up @@ -114,6 +115,4 @@ in {
genode = filterDoubles predicates.isGenode;

embedded = filterDoubles predicates.isNone;

mesaPlatforms = ["i686-linux" "x86_64-linux" "x86_64-darwin" "armv5tel-linux" "armv6l-linux" "armv7l-linux" "armv7a-linux" "aarch64-linux" "powerpc64-linux" "powerpc64le-linux" "aarch64-darwin" "riscv64-linux"];
}
5 changes: 3 additions & 2 deletions lib/systems/platforms.nix
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,9 @@ rec {
target = "zImage";
};
gcc = {
arch = "armv6";
fpu = "vfp";
# https://en.wikipedia.org/wiki/Raspberry_Pi#Specifications
arch = "armv6kz";
fpu = "vfpv2";
};
};

Expand Down
28 changes: 24 additions & 4 deletions lib/tests/misc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,26 @@ runTests {
expected = [ "A" "B" ];
};

testEscapeShellArg = {
expr = strings.escapeShellArg "esc'ape\nme";
expected = "'esc'\\''ape\nme'";
};

testEscapeShellArgEmpty = {
expr = strings.escapeShellArg "";
expected = "''";
};

testEscapeShellArgs = {
expr = strings.escapeShellArgs ["one" "two three" "four'five"];
expected = "one 'two three' 'four'\\''five'";
};

testEscapeShellArgsUnicode = {
expr = strings.escapeShellArg "á";
expected = "'á'";
};

testSplitStringsDerivation = {
expr = take 3 (strings.splitString "/" (derivation {
name = "name";
Expand Down Expand Up @@ -569,12 +589,12 @@ runTests {
'';
expected = ''
STRing01='just a '\'''string'\''''
declare -a _array_=('with' 'more strings')
declare -a _array_=(with 'more strings')
declare -A assoc=(['with some']='strings
possibly newlines
')
drv='/drv'
path='/path'
drv=/drv
path=/path
stringable='hello toString'
'';
};
Expand Down Expand Up @@ -1754,7 +1774,7 @@ runTests {
verbose = true;
};

expected = "'-X' 'PUT' '--data' '{\"id\":0}' '--retry' '3' '--url' 'https://example.com/foo' '--url' 'https://example.com/bar' '--verbose'";
expected = "-X PUT --data '{\"id\":0}' --retry 3 --url https://example.com/foo --url https://example.com/bar --verbose";
};

testSanitizeDerivationNameLeadingDots = testSanitizeDerivationName {
Expand Down
26 changes: 17 additions & 9 deletions lib/tests/modules.sh
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,14 @@ checkConfigOutput '^true$' "$@" ./define-enable.nix ./define-attrsOfSub-if-foo-e
checkConfigOutput '^true$' "$@" ./define-enable.nix ./define-attrsOfSub-foo-if-enable.nix
checkConfigOutput '^true$' "$@" ./define-enable.nix ./define-attrsOfSub-foo-enable-if.nix

# Check importApply
checkConfigOutput '"abc"' config.value ./importApply.nix
# importApply does not set a key.
# Disabling the function file is not sufficient, because importApply can't reasonably assume that the key is unique.
# e.g. user may call it multiple times with different arguments and expect each of the module to apply.
# While this is excusable for the disabledModules aspect, it is not for the deduplication of modules.
checkConfigOutput '"abc"' config.value ./importApply-disabling.nix

# Check disabledModules with config definitions and option declarations.
set -- config.enable ./define-enable.nix ./declare-enable.nix
checkConfigOutput '^true$' "$@"
Expand Down Expand Up @@ -542,21 +550,21 @@ checkConfigOutput '^"pear\\npear"$' config.twice.raw ./merge-module-with-key.nix

# Declaration positions
# Line should be present for direct options
checkConfigOutput '^10$' options.imported.line10.declarationPositions.0.line ./declaration-positions.nix
checkConfigOutput '/declaration-positions.nix"$' options.imported.line10.declarationPositions.0.file ./declaration-positions.nix
checkConfigOutput '^14$' options.imported.line14.declarationPositions.0.line ./declaration-positions.nix
checkConfigOutput '/declaration-positions.nix"$' options.imported.line14.declarationPositions.0.file ./declaration-positions.nix
# Generated options may not have line numbers but they will at least get the
# right file
checkConfigOutput '/declaration-positions.nix"$' options.generated.line18.declarationPositions.0.file ./declaration-positions.nix
checkConfigOutput '^null$' options.generated.line18.declarationPositions.0.line ./declaration-positions.nix
checkConfigOutput '/declaration-positions.nix"$' options.generated.line22.declarationPositions.0.file ./declaration-positions.nix
checkConfigOutput '^null$' options.generated.line22.declarationPositions.0.line ./declaration-positions.nix
# Submodules don't break it
checkConfigOutput '^39$' config.submoduleLine34.submodDeclLine39.0.line ./declaration-positions.nix
checkConfigOutput '/declaration-positions.nix"$' config.submoduleLine34.submodDeclLine39.0.file ./declaration-positions.nix
checkConfigOutput '^45$' config.submoduleLine38.submodDeclLine45.0.line ./declaration-positions.nix
checkConfigOutput '/declaration-positions.nix"$' config.submoduleLine38.submodDeclLine45.0.file ./declaration-positions.nix
# New options under freeform submodules get collected into the parent submodule
# (consistent with .declarations behaviour, but weird; notably appears in system.build)
checkConfigOutput '^34|23$' options.submoduleLine34.declarationPositions.0.line ./declaration-positions.nix
checkConfigOutput '^34|23$' options.submoduleLine34.declarationPositions.1.line ./declaration-positions.nix
checkConfigOutput '^38|27$' options.submoduleLine38.declarationPositions.0.line ./declaration-positions.nix
checkConfigOutput '^38|27$' options.submoduleLine38.declarationPositions.1.line ./declaration-positions.nix
# nested options work
checkConfigOutput '^30$' options.nested.nestedLine30.declarationPositions.0.line ./declaration-positions.nix
checkConfigOutput '^34$' options.nested.nestedLine34.declarationPositions.0.line ./declaration-positions.nix

cat <<EOF
====== module tests ======
Expand Down
Loading

0 comments on commit 7f0b9e4

Please sign in to comment.