Skip to content

Commit 335eb70

Browse files
Merge pull request #156 from PerchunPak/allow-function-patches
Allow patching with functions that accept pkgs
2 parents afcb15b + 1e091a9 commit 335eb70

5 files changed

Lines changed: 129 additions & 14 deletions

File tree

devShell.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ devshell.mkShell {
7575
}
7676

7777
(test "channel-patching")
78+
(test "channel-patching-using-func")
7879
(test "derivation-outputs")
7980
(test "hosts-config")
8081
(test "overlays-flow")

flake.nix

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@
4747
rhs;
4848

4949
patchChannel = system: channel: patches:
50+
let
51+
pkgs = (import channel { inherit system; }).pkgs;
52+
in
5053
if patches == [ ] then channel else
51-
(import channel { inherit system; }).pkgs.applyPatches {
54+
pkgs.applyPatches {
5255
name = if channel ? shortRev then "nixpkgs-patched-${channel.shortRev}" else "nixpkgs-patched";
5356
src = channel;
54-
patches = patches;
57+
patches = map (patch: if pkgs.lib.isFunction patch then patch pkgs else patch) patches;
5558
};
56-
5759
};
5860
};
5961
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
inputs.utils.url = path:../../;
3+
inputs.nixpkgs.url = github:NixOS/nixpkgs/;
4+
5+
outputs = inputs@{ self, nixpkgs, utils }:
6+
utils.lib.mkFlake {
7+
inherit self inputs;
8+
supportedSystems = [ "x86_64-linux" ];
9+
10+
11+
12+
13+
#################
14+
### Test Data ###
15+
#################
16+
17+
channelsConfig.allowBroken = true;
18+
19+
channels.nixpkgs = {
20+
input = nixpkgs;
21+
patches = [
22+
(pkgs: pkgs.runCommand "myNixpkgsPatch.patch" { } "cp ${./myNixpkgsPatch.patch} $out")
23+
];
24+
config.allowUnfree = true;
25+
};
26+
27+
hosts.PatchedHost.modules = [
28+
({ lib, ... }: {
29+
patchedModule.test = lib.patchedFunction "using patched module via patched function";
30+
31+
# To keep Nix from complaining
32+
boot.loader.grub.devices = [ "nodev" ];
33+
fileSystems."/" = { device = "test"; fsType = "ext4"; };
34+
})
35+
];
36+
37+
38+
outputsBuilder = channels: {
39+
40+
packages = {
41+
# Using patched channel
42+
inherit (channels.nixpkgs) flake-utils-plus-test;
43+
};
44+
45+
46+
47+
######################
48+
### Test execution ###
49+
######################
50+
51+
checks =
52+
let
53+
inherit (utils.lib.check-utils channels.nixpkgs) hasKey isEqual;
54+
hostConfig = self.nixosConfigurations.PatchedHost.config;
55+
in
56+
{
57+
58+
# Patched package gets passed to `packageBuilder`
59+
patchedPackageGetsPassedToBuilders = isEqual self.packages.x86_64-linux.flake-utils-plus-test.pname "coreutils";
60+
61+
# Modules (and lib) from patched nixpkgs are used
62+
patchedModuleAndFunctionWorks = isEqual hostConfig.patchedModule.test "using patched module via patched function";
63+
64+
# `channelsConfig.*` is used
65+
globalChannelConfigWorks = hasKey hostConfig.nixpkgs.pkgs.config "allowBroken";
66+
67+
# `channels.nixpkgs.config.*` is also used
68+
channelSpecificConfigWorks = hasKey hostConfig.nixpkgs.pkgs.config "allowUnfree";
69+
70+
};
71+
};
72+
73+
74+
};
75+
}
76+
77+
78+
79+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
diff --git a/lib/default.nix b/lib/default.nix
2+
index 751738a98965..b689f7744bac 100644
3+
--- a/lib/default.nix
4+
+++ b/lib/default.nix
5+
@@ -48,6 +48,7 @@ let
6+
callLibs = file: import file { lib = self; };
7+
in
8+
{
9+
+ patchedFunction = x: x;
10+
11+
# often used, or depending on very little
12+
trivial = callLibs ./trivial.nix;
13+
diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix
14+
index 6995f3ba1ed2..49adcce57c6f 100644
15+
--- a/nixos/modules/module-list.nix
16+
+++ b/nixos/modules/module-list.nix
17+
@@ -2039,4 +2039,10 @@
18+
./image/repart.nix
19+
];
20+
}
21+
+ ({ lib, config, ... }: {
22+
+ options.patchedModule.test = lib.mkOption {
23+
+ default = null;
24+
+ example = "test";
25+
+ };
26+
+ })
27+
]
28+
diff --git a/pkgs/by-name/fl/flake-utils-plus-test/package.nix b/pkgs/by-name/fl/flake-utils-plus-test/package.nix
29+
new file mode 100644
30+
index 000000000000..ad5323375baf
31+
--- /dev/null
32+
+++ b/pkgs/by-name/fl/flake-utils-plus-test/package.nix
33+
@@ -0,0 +1 @@
34+
+{ coreutils }: coreutils

tests/channel-patching/myNixpkgsPatch.patch

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
diff --git a/lib/default.nix b/lib/default.nix
2-
index f931524002f2..5299719bfd92 100644
2+
index 751738a98965..b689f7744bac 100644
33
--- a/lib/default.nix
44
+++ b/lib/default.nix
5-
@@ -10,6 +10,8 @@ let
6-
lib = makeExtensible (self: let
7-
callLibs = file: import file { lib = self; };
8-
in {
9-
+ patchedFunction = x: x;
10-
+
5+
@@ -48,6 +48,7 @@ let
6+
callLibs = file: import file { lib = self; };
7+
in
8+
{
9+
+ patchedFunction = x: x;
1110

12-
# often used, or depending on very little
13-
trivial = callLibs ./trivial.nix;
11+
# often used, or depending on very little
12+
trivial = callLibs ./trivial.nix;
1413
diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix
15-
index bd3b27c00b55..3cfde568a714 100644
14+
index 6995f3ba1ed2..49adcce57c6f 100644
1615
--- a/nixos/modules/module-list.nix
1716
+++ b/nixos/modules/module-list.nix
18-
@@ -1818,4 +1818,10 @@
17+
@@ -2039,4 +2039,10 @@
1918
./image/repart.nix
2019
];
2120
}

0 commit comments

Comments
 (0)