Skip to content

Commit 871f9c6

Browse files
committed
unify nix helpers
no reason to implement hooks in multiple places
1 parent 1e5ffcc commit 871f9c6

File tree

6 files changed

+82
-84
lines changed

6 files changed

+82
-84
lines changed

chainloader.nix

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
let
2121
includeos = pkgs.pkgsIncludeOS.includeos;
2222
stdenv = pkgs.pkgsIncludeOS.stdenv;
23+
24+
suppress = includeos.pkgs.callPackage ./nix/suppress.nix {};
25+
ccache = includeos.pkgs.callPackage ./includeos-ccache.nix {};
2326
in
2427

2528
assert (stdenv.targetPlatform.system != "i686-linux") ->
@@ -48,5 +51,5 @@ stdenv.mkDerivation rec {
4851
nativeBuildInputs = [
4952
pkgs.buildPackages.cmake
5053
pkgs.buildPackages.nasm
51-
] ++ [ pkgs.pkgsIncludeOS.suppressTargetWarningHook ];
54+
] ++ [ suppress.targetWarningHook ];
5255
}

nix/ccache.nix

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{ pkgs
2+
, cc
3+
, ccacheDir ? "/nix/var/cache/ccache"
4+
, showNotice ? true
5+
}:
6+
let
7+
noticeHook =
8+
if showNotice then
9+
pkgs.writeTextFile {
10+
name = "ccache-notice-hook";
11+
destination = "/nix-support/setup-hook";
12+
text = ''
13+
echo "====="
14+
echo "ccache is enabled!"
15+
echo "Disable with: --arg withCcache false"
16+
echo "====="
17+
'';
18+
}
19+
else
20+
null;
21+
22+
wrapper = pkgs.ccacheWrapper.override {
23+
inherit cc;
24+
25+
extraConfig = ''
26+
export CCACHE_DIR="${ccacheDir}"
27+
if [ ! -d "$CCACHE_DIR" ]; then
28+
echo "====="
29+
echo "Directory '$CCACHE_DIR' does not exist"
30+
echo " doas mkdir -m0770 '$CCACHE_DIR' && doas chown root:nixbld '$CCACHE_DIR'"
31+
echo "Or disable with --arg withCcache false"
32+
echo "====="
33+
exit 1
34+
fi
35+
if [ ! -w "$CCACHE_DIR" ]; then
36+
echo "====="
37+
echo "Directory '$CCACHE_DIR' exists but isn't writable by $(whoami)"
38+
echo "====="
39+
exit 1
40+
fi
41+
export CCACHE_COMPRESS=1
42+
export CCACHE_UMASK=007
43+
export CCACHE_SLOPPINESS=random_seed
44+
'';
45+
};
46+
in
47+
{
48+
inherit wrapper noticeHook;
49+
}

nix/suppress.nix

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{ pkgs }:
2+
let
3+
targetWarningHook = pkgs.writeTextFile {
4+
name = "suppress-target-warning-hook";
5+
destination = "/nix-support/setup-hook";
6+
text = ''
7+
# see https://github.com/NixOS/nixpkgs/issues/395191
8+
# delete this hook and downstream references once resolved
9+
10+
export NIX_CC_WRAPPER_SUPPRESS_TARGET_WARNING=1
11+
'';
12+
};
13+
in
14+
{
15+
inherit targetWarningHook;
16+
}

overlay.nix

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -59,32 +59,12 @@ final: prev: {
5959

6060
pkgsIncludeOS = prev.pkgsStatic.lib.makeScope prev.pkgsStatic.newScope (self:
6161
let
62-
ccacheNoticeHook = prev.writeTextFile {
63-
name = "ccache-notice-hook";
64-
destination = "/nix-support/setup-hook";
65-
text = ''
66-
echo "====="
67-
echo "ccache is enabled!"
68-
echo "If you run into any issues, try: --arg withCcache false"
69-
echo "It's recommended to run tests with ccache disabled to avoid cache incoherencies."
70-
echo "====="
71-
'';
72-
};
73-
74-
suppressTargetWarningHook = prev.writeTextFile {
75-
name = "suppress-target-warning-hook";
76-
destination = "/nix-support/setup-hook";
77-
text = ''
78-
# see https://github.com/NixOS/nixpkgs/issues/395191
79-
# delete this hook and downstream references once resolved
62+
ccache = import ./nix/ccache.nix { pkgs = prev; inherit (self.stdenv) cc; };
8063

81-
export NIX_CC_WRAPPER_SUPPRESS_TARGET_WARNING=1
82-
'';
83-
};
64+
suppress = import ./nix/suppress.nix { pkgs = prev; };
8465
in {
8566
# self.callPackage will use this stdenv.
8667
stdenv = final.stdenvIncludeOS.includeos_stdenv;
87-
inherit suppressTargetWarningHook;
8868

8969
# Deps
9070
botan2 = self.callPackage ./deps/botan/default.nix { };
@@ -95,36 +75,6 @@ final: prev: {
9575

9676
vmbuild = self.callPackage ./vmbuild.nix { };
9777

98-
ccacheWrapper = prev.ccacheWrapper.override {
99-
inherit (self.stdenv) cc;
100-
extraConfig = ''
101-
export CCACHE_DIR="/nix/var/cache/ccache"
102-
if [ ! -d "$CCACHE_DIR" ]; then
103-
echo "====="
104-
echo "Directory '$CCACHE_DIR' does not exist"
105-
echo "Please create it with:"
106-
echo " sudo mkdir -m0770 '$CCACHE_DIR'"
107-
echo " sudo chown root:nixbld '$CCACHE_DIR'"
108-
echo ""
109-
echo 'Alternatively, disable ccache with `--arg withCcache false`'
110-
echo "====="
111-
exit 1
112-
fi
113-
if [ ! -w "$CCACHE_DIR" ]; then
114-
echo "====="
115-
echo "Directory '$CCACHE_DIR' exists, but is not accessible for user $(whoami)"
116-
echo "Please verify its access permissions"
117-
echo 'Alternatively, disable ccache with `--arg withCcache false`'
118-
echo "====="
119-
exit 1
120-
fi
121-
122-
export CCACHE_COMPRESS=1
123-
export CCACHE_UMASK=007
124-
export CCACHE_SLOPPINESS=random_seed
125-
'';
126-
};
127-
12878
# IncludeOS
12979
includeos = self.stdenv.mkDerivation rec {
13080
enableParallelBuilding = true;
@@ -155,8 +105,8 @@ final: prev: {
155105
nativeBuildInputs = [
156106
prev.buildPackages.cmake
157107
prev.buildPackages.nasm
158-
] ++ prev.lib.optionals disableTargetWarning [suppressTargetWarningHook]
159-
++ prev.lib.optionals withCcache [self.ccacheWrapper ccacheNoticeHook];
108+
] ++ prev.lib.optionals disableTargetWarning [suppress.targetWarningHook]
109+
++ prev.lib.optionals withCcache [ccache.wrapper ccache.noticeHook];
160110

161111
buildInputs = [
162112
self.libfmt

unikernel.nix

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ let
3838
includeos.vmrunner
3939
else
4040
includeos.pkgs.callPackage (builtins.toPath /. + vmrunner) {};
41+
42+
suppress = includeos.pkgs.callPackage ./nix/suppress.nix {};
43+
ccache = includeos.pkgs.callPackage ./nix/ccache.nix {};
4144
in
4245
includeos.stdenv.mkDerivation rec {
4346
pname = "includeos_example";
@@ -49,7 +52,7 @@ includeos.stdenv.mkDerivation rec {
4952
nativeBuildInputs = [
5053
includeos.pkgs.buildPackages.nasm
5154
includeos.pkgs.buildPackages.cmake
52-
] ++ [ includeos.pkgs.pkgsIncludeOS.suppressTargetWarningHook ];
55+
] ++ [ suppress.targetWarningHook ];
5356

5457
buildInputs = [
5558
includeos

unittests.nix

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,22 @@
33
stdenv ? pkgs.llvmPackages_19.libcxxStdenv,
44
withCcache ? false,
55
}:
6+
let
7+
ccache = pkgs.callPackage ./nix/ccache.nix { };
8+
in
69
stdenv.mkDerivation rec {
710
pname = "unittests";
811
version = "dev";
912
enableParallelBuilding = true;
1013

1114
sourceRoot = "test";
1215

13-
ccacheWrapper = pkgs.ccacheWrapper.override {
14-
inherit (stdenv) cc;
15-
extraConfig = ''
16-
export CCACHE_COMPRESS=1
17-
export CCACHE_DIR="/nix/var/cache/ccache"
18-
export CCACHE_UMASK=007
19-
export CCACHE_SLOPPINESS=random_seed
20-
if [ ! -d "$CCACHE_DIR" ]; then
21-
echo "====="
22-
echo "Directory '$CCACHE_DIR' does not exist"
23-
echo "Please create it with:"
24-
echo " sudo mkdir -m0770 '$CCACHE_DIR'"
25-
echo " sudo chown root:nixbld '$CCACHE_DIR'"
26-
echo "====="
27-
exit 1
28-
fi
29-
if [ ! -w "$CCACHE_DIR" ]; then
30-
echo "====="
31-
echo "Directory '$CCACHE_DIR' is not accessible for user $(whoami)"
32-
echo "Please verify its access permissions"
33-
echo "====="
34-
exit 1
35-
fi
36-
'';
37-
};
38-
3916
srcs = [
4017
./test
4118
./src
4219
./api
4320
./lib
44-
];
21+
];
4522

4623
hardeningDisable = [ "all" ];
4724
cmakeBuildType = "Debug";
@@ -59,7 +36,7 @@ stdenv.mkDerivation rec {
5936
pkgs.buildPackages.cmake
6037
pkgs.buildPackages.valgrind
6138
pkgs.buildPackages.clang-tools
62-
] ++ pkgs.lib.optionals withCcache [ccacheWrapper];
39+
] ++ pkgs.lib.optionals withCcache [ccache.wrapper];
6340

6441
buildInputs = [
6542
pkgs.rapidjson

0 commit comments

Comments
 (0)