Skip to content

Commit

Permalink
Merge master into staging-next
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Apr 19, 2024
2 parents da03c8f + 193037d commit 11b87cb
Show file tree
Hide file tree
Showing 108 changed files with 1,243 additions and 505 deletions.
48 changes: 43 additions & 5 deletions lib/attrsets.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

let
inherit (builtins) head length;
inherit (lib.trivial) mergeAttrs warn;
inherit (lib.trivial) isInOldestRelease mergeAttrs warn warnIf;
inherit (lib.strings) concatStringsSep concatMapStringsSep escapeNixIdentifier sanitizeDerivationName;
inherit (lib.lists) foldr foldl' concatMap elemAt all partition groupBy take foldl;
in
Expand Down Expand Up @@ -885,15 +885,15 @@ rec {
# Type
```
cartesianProductOfSets :: AttrSet -> [AttrSet]
cartesianProduct :: AttrSet -> [AttrSet]
```
# Examples
:::{.example}
## `lib.attrsets.cartesianProductOfSets` usage example
## `lib.attrsets.cartesianProduct` usage example
```nix
cartesianProductOfSets { a = [ 1 2 ]; b = [ 10 20 ]; }
cartesianProduct { a = [ 1 2 ]; b = [ 10 20 ]; }
=> [
{ a = 1; b = 10; }
{ a = 1; b = 20; }
Expand All @@ -904,7 +904,7 @@ rec {
:::
*/
cartesianProductOfSets =
cartesianProduct =
attrsOfLists:
foldl' (listOfAttrs: attrName:
concatMap (attrs:
Expand All @@ -913,6 +913,40 @@ rec {
) [{}] (attrNames attrsOfLists);


/**
Return the result of function f applied to the cartesian product of attribute set value combinations.
Equivalent to using cartesianProduct followed by map.
# Inputs
`f`
: A function, given an attribute set, it returns a new value.
`attrsOfLists`
: Attribute set with attributes that are lists of values
# Type
```
mapCartesianProduct :: (AttrSet -> a) -> AttrSet -> [a]
```
# Examples
:::{.example}
## `lib.attrsets.mapCartesianProduct` usage example
```nix
mapCartesianProduct ({a, b}: "${a}-${b}") { a = [ "1" "2" ]; b = [ "3" "4" ]; }
=> [ "1-3" "1-4" "2-3" "2-4" ]
```
:::
*/
mapCartesianProduct = f: attrsOfLists: map f (cartesianProduct attrsOfLists);

/**
Utility function that creates a `{name, value}` pair as expected by `builtins.listToAttrs`.
Expand Down Expand Up @@ -1999,4 +2033,8 @@ rec {
# DEPRECATED
zip = warn
"lib.zip is a deprecated alias of lib.zipAttrsWith." zipAttrsWith;

# DEPRECATED
cartesianProductOfSets = warnIf (isInOldestRelease 2405)
"lib.cartesianProductOfSets is a deprecated alias of lib.cartesianProduct." cartesianProduct;
}
4 changes: 2 additions & 2 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ let
zipAttrsWithNames zipAttrsWith zipAttrs recursiveUpdateUntil
recursiveUpdate matchAttrs mergeAttrsList overrideExisting showAttrPath getOutput
getBin getLib getDev getMan chooseDevOutputs zipWithNames zip
recurseIntoAttrs dontRecurseIntoAttrs cartesianProductOfSets
updateManyAttrsByPath;
recurseIntoAttrs dontRecurseIntoAttrs cartesianProduct cartesianProductOfSets
mapCartesianProduct updateManyAttrsByPath;
inherit (self.lists) singleton forEach foldr fold foldl foldl' imap0 imap1
concatMap flatten remove findSingle findFirst any all count
optional optionals toList range replicate partition zipListsWith zipLists
Expand Down
22 changes: 19 additions & 3 deletions lib/lists.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1688,16 +1688,32 @@ rec {
## `lib.lists.crossLists` usage example
```nix
crossLists (x:y: "${toString x}${toString y}") [[1 2] [3 4]]
crossLists (x: y: "${toString x}${toString y}") [[1 2] [3 4]]
=> [ "13" "14" "23" "24" ]
```
The following function call is equivalent to the one deprecated above:
```nix
mapCartesianProduct (x: "${toString x.a}${toString x.b}") { a = [1 2]; b = [3 4]; }
=> [ "13" "14" "23" "24" ]
```
:::
*/
crossLists = warn
"lib.crossLists is deprecated, use lib.cartesianProductOfSets instead."
(f: foldl (fs: args: concatMap (f: map f args) fs) [f]);
''lib.crossLists is deprecated, use lib.mapCartesianProduct instead.
For example, the following function call:
nix-repl> lib.crossLists (x: y: x+y) [[1 2] [3 4]]
[ 4 5 5 6 ]
Can now be replaced by the following one:
nix-repl> lib.mapCartesianProduct ({x,y}: x+y) { x = [1 2]; y = [3 4]; }
[ 4 5 5 6 ]
''
(f: foldl (fs: args: concatMap (f: map f args) fs) [f]);

/**
Remove duplicate elements from the `list`. O(n^2) complexity.
Expand Down
50 changes: 35 additions & 15 deletions lib/tests/misc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ let
boolToString
callPackagesWith
callPackageWith
cartesianProductOfSets
cartesianProduct
cli
composeExtensions
composeManyExtensions
Expand Down Expand Up @@ -71,10 +71,10 @@ let
makeIncludePath
makeOverridable
mapAttrs
mapCartesianProduct
matchAttrs
mergeAttrs
meta
mkOption
mod
nameValuePair
optionalDrvAttr
Expand Down Expand Up @@ -117,7 +117,6 @@ let
expr = (builtins.tryEval expr).success;
expected = true;
};
testingDeepThrow = expr: testingThrow (builtins.deepSeq expr expr);

testSanitizeDerivationName = { name, expected }:
let
Expand Down Expand Up @@ -1415,7 +1414,7 @@ runTests {
};

testToPrettyMultiline = {
expr = mapAttrs (const (generators.toPretty { })) rec {
expr = mapAttrs (const (generators.toPretty { })) {
list = [ 3 4 [ false ] ];
attrs = { foo = null; bar.foo = "baz"; };
newlinestring = "\n";
Expand All @@ -1429,7 +1428,7 @@ runTests {
there
test'';
};
expected = rec {
expected = {
list = ''
[
3
Expand Down Expand Up @@ -1467,13 +1466,10 @@ runTests {
expected = "«foo»";
};

testToPlist =
let
deriv = derivation { name = "test"; builder = "/bin/sh"; system = "aarch64-linux"; };
in {
testToPlist = {
expr = mapAttrs (const (generators.toPlist { })) {
value = {
nested.values = rec {
nested.values = {
int = 42;
float = 0.1337;
bool = true;
Expand Down Expand Up @@ -1686,30 +1682,30 @@ runTests {
};

testCartesianProductOfEmptySet = {
expr = cartesianProductOfSets {};
expr = cartesianProduct {};
expected = [ {} ];
};

testCartesianProductOfOneSet = {
expr = cartesianProductOfSets { a = [ 1 2 3 ]; };
expr = cartesianProduct { a = [ 1 2 3 ]; };
expected = [ { a = 1; } { a = 2; } { a = 3; } ];
};

testCartesianProductOfTwoSets = {
expr = cartesianProductOfSets { a = [ 1 ]; b = [ 10 20 ]; };
expr = cartesianProduct { a = [ 1 ]; b = [ 10 20 ]; };
expected = [
{ a = 1; b = 10; }
{ a = 1; b = 20; }
];
};

testCartesianProductOfTwoSetsWithOneEmpty = {
expr = cartesianProductOfSets { a = [ ]; b = [ 10 20 ]; };
expr = cartesianProduct { a = [ ]; b = [ 10 20 ]; };
expected = [ ];
};

testCartesianProductOfThreeSets = {
expr = cartesianProductOfSets {
expr = cartesianProduct {
a = [ 1 2 3 ];
b = [ 10 20 30 ];
c = [ 100 200 300 ];
Expand Down Expand Up @@ -1753,6 +1749,30 @@ runTests {
];
};

testMapCartesianProductOfOneSet = {
expr = mapCartesianProduct ({a}: a * 2) { a = [ 1 2 3 ]; };
expected = [ 2 4 6 ];
};

testMapCartesianProductOfTwoSets = {
expr = mapCartesianProduct ({a,b}: a + b) { a = [ 1 ]; b = [ 10 20 ]; };
expected = [ 11 21 ];
};

testMapCartesianProcutOfTwoSetsWithOneEmpty = {
expr = mapCartesianProduct (x: x.a + x.b) { a = [ ]; b = [ 10 20 ]; };
expected = [ ];
};

testMapCartesianProductOfThreeSets = {
expr = mapCartesianProduct ({a,b,c}: a + b + c) {
a = [ 1 2 3 ];
b = [ 10 20 30 ];
c = [ 100 200 300 ];
};
expected = [ 111 211 311 121 221 321 131 231 331 112 212 312 122 222 322 132 232 332 113 213 313 123 223 323 133 233 333 ];
};

# The example from the showAttrPath documentation
testShowAttrPathExample = {
expr = showAttrPath [ "foo" "10" "bar" ];
Expand Down
6 changes: 6 additions & 0 deletions maintainers/maintainer-list.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13394,6 +13394,12 @@
fingerprint = "64BE BF11 96C3 DD7A 443E 8314 1DC0 82FA DE5B A863";
}];
};
mlaradji = {
name = "Mohamed Laradji";
email = "[email protected]";
github = "mlaradji";
githubId = 33703663;
};
mlatus = {
email = "[email protected]";
github = "Ninlives";
Expand Down
5 changes: 4 additions & 1 deletion nixos/modules/services/misc/paperless.nix
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,12 @@ in
apply = pkg: pkg.override {
tesseract5 = pkg.tesseract5.override {
# always enable detection modules
# tesseract fails to build when eng is not present
enableLanguages = if cfg.settings ? PAPERLESS_OCR_LANGUAGE then
[ "equ" "osd" ]
lists.unique (
[ "equ" "osd" "eng" ]
++ lib.splitString "+" cfg.settings.PAPERLESS_OCR_LANGUAGE
)
else null;
};
};
Expand Down
41 changes: 37 additions & 4 deletions nixos/modules/services/misc/podgrab.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{ config, lib, pkgs, ... }:
let
cfg = config.services.podgrab;

stateDir = "/var/lib/podgrab";
in
{
options.services.podgrab = with lib; {
Expand All @@ -22,28 +24,59 @@ in
example = 4242;
description = "The port on which Podgrab will listen for incoming HTTP traffic.";
};

dataDirectory = mkOption {
type = types.path;
default = "${stateDir}/data";
example = "/mnt/podcasts";
description = "Directory to store downloads.";
};

user = mkOption {
type = types.str;
default = "podgrab";
description = "User under which Podgrab runs, and which owns the download directory.";
};

group = mkOption {
type = types.str;
default = "podgrab";
description = "Group under which Podgrab runs, and which owns the download directory.";
};
};

config = lib.mkIf cfg.enable {
systemd.tmpfiles.settings."10-pyload" = {
${cfg.dataDirectory}.d = { inherit (cfg) user group; };
};

systemd.services.podgrab = {
description = "Podgrab podcast manager";
wantedBy = [ "multi-user.target" ];
environment = {
CONFIG = "/var/lib/podgrab/config";
DATA = "/var/lib/podgrab/data";
CONFIG = "${stateDir}/config";
DATA = cfg.dataDirectory;
GIN_MODE = "release";
PORT = toString cfg.port;
};
serviceConfig = {
DynamicUser = true;
User = cfg.user;
Group = cfg.group;
EnvironmentFile = lib.optionals (cfg.passwordFile != null) [
cfg.passwordFile
];
ExecStart = "${pkgs.podgrab}/bin/podgrab";
WorkingDirectory = "${pkgs.podgrab}/share";
StateDirectory = [ "podgrab/config" "podgrab/data" ];
StateDirectory = [ "podgrab/config" ];
};
};

users.users.podgrab = lib.mkIf (cfg.user == "podgrab") {
isSystemUser = true;
group = cfg.group;
};

users.groups.podgrab = lib.mkIf (cfg.group == "podgrab") { };
};

meta.maintainers = with lib.maintainers; [ ambroisie ];
Expand Down
4 changes: 2 additions & 2 deletions nixos/modules/services/x11/display-managers/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ in
in
# We will generate every possible pair of WM and DM.
concatLists (
builtins.map
lib.mapCartesianProduct
({dm, wm}: let
sessionName = "${dm.name}${optionalString (wm.name != "none") ("+" + wm.name)}";
script = xsession dm wm;
Expand Down Expand Up @@ -312,7 +312,7 @@ in
providedSessions = [ sessionName ];
})
)
(cartesianProductOfSets { dm = dms; wm = wms; })
{ dm = dms; wm = wms; }
);
};

Expand Down
1 change: 1 addition & 0 deletions nixos/tests/paperless.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import ./make-test-python.nix ({ lib, ... }: {
};
services.paperless.settings = {
PAPERLESS_DBHOST = "/run/postgresql";
PAPERLESS_OCR_LANGUAGE = "deu";
};
};
}; in self;
Expand Down
2 changes: 1 addition & 1 deletion nixos/tests/predictable-interface-names.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

let
inherit (import ../lib/testing-python.nix { inherit system pkgs; }) makeTest;
testCombinations = pkgs.lib.cartesianProductOfSets {
testCombinations = pkgs.lib.cartesianProduct {
predictable = [true false];
withNetworkd = [true false];
systemdStage1 = [true false];
Expand Down
Loading

0 comments on commit 11b87cb

Please sign in to comment.