Skip to content

Commit cad0b17

Browse files
committed
fix(flake.nix): link rcfile to bashrc for login and non-login shells
Change-Id: I82259a2c620b1711440e79c58bbc06080d888c9a Signed-off-by: Thomas Kosiewski <tk@coder.com>
1 parent d389744 commit cad0b17

File tree

2 files changed

+70
-18
lines changed

2 files changed

+70
-18
lines changed

flake.nix

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
drpc.defaultPackage.${system}
8686
formatter
8787
fzf
88-
gcc
88+
gcc13
8989
gdk
9090
getopt
9191
gh
@@ -212,10 +212,9 @@
212212
devShells = {
213213
default = pkgs.mkShell {
214214
buildInputs = devShellPackages;
215-
shellHook = ''
216-
export PLAYWRIGHT_BROWSERS_PATH=${pkgs.playwright-driver.browsers}
217-
export PLAYWRIGHT_SKIP_VALIDATE_HOST_REQUIREMENTS=true
218-
'';
215+
216+
PLAYWRIGHT_BROWSERS_PATH = pkgs.playwright-driver.browsers;
217+
PLAYWRIGHT_SKIP_VALIDATE_HOST_REQUIREMENTS = true;
219218

220219
LOCALE_ARCHIVE =
221220
with pkgs;
@@ -239,21 +238,29 @@
239238
aarch64-windows = buildFat "windows_arm64.exe";
240239
}
241240
// (pkgs.lib.optionalAttrs pkgs.stdenv.isLinux {
242-
dev_image = docker.buildNixShellImage {
241+
dev_image = docker.buildNixShellImage rec {
243242
name = "codercom/oss-dogfood-nix";
244243
tag = "latest-${system}";
245244

245+
# (ThomasK33): Workaround for images with too many layers (>64 layers) causing sysbox
246+
# to have issues on dogfood envs.
246247
maxLayers = 32;
247248

249+
uname = "coder";
250+
homeDirectory = "/home/${uname}";
251+
248252
drv = devShells.default.overrideAttrs (oldAttrs: {
249-
# (ThomasK33): Workaround for images with too many layers (>64 layers) causing sysbox
250-
# to have issues on dogfood envs.
251253
buildInputs =
252-
oldAttrs.buildInputs
253-
++ (with pkgs; [
254-
nix
254+
(with pkgs; [
255+
busybox
255256
coreutils
256-
]);
257+
nix
258+
curl.bin # Ensure the actual curl binary is included in the PATH
259+
glibc.bin # Ensure the glibc binaries are included in the PATH
260+
binutils # ld and strings
261+
filebrowser # Ensure that we're not redownloading filebrowser on each launch
262+
])
263+
++ oldAttrs.buildInputs;
257264
});
258265
};
259266
});

nix/docker.nix

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
storeDir ? builtins.storeDir,
1818
pigz,
1919
zstd,
20+
stdenv,
21+
glibc,
2022
}:
2123
let
2224
inherit (lib)
@@ -70,6 +72,7 @@ let
7072
command ? null,
7173
run ? null,
7274
maxLayers ? 100,
75+
uname ? "nixbld",
7376
}:
7477
assert lib.assertMsg (!(drv.drvAttrs.__structuredAttrs or false))
7578
"streamNixShellImage: Does not work with the derivation ${drv.name} because it uses __structuredAttrs";
@@ -83,7 +86,14 @@ let
8386
exec ${lib.escapeShellArg (valueToString drv.drvAttrs.builder)} ${lib.escapeShellArgs (map valueToString drv.drvAttrs.args)}
8487
'';
8588

86-
staticPath = "${dirOf shell}:${lib.makeBinPath [ builder ]}";
89+
staticPath = "${dirOf shell}:${
90+
lib.makeBinPath (
91+
lib.flatten [
92+
builder
93+
drv.buildInputs
94+
]
95+
)
96+
}";
8797

8898
# https://github.com/NixOS/nix/blob/2.8.0/src/nix-build/nix-build.cc#L493-L526
8999
rcfile = writeText "nix-shell-rc" ''
@@ -109,6 +119,15 @@ let
109119
''}
110120
'';
111121

122+
nixConfFile = writeText "nix-conf" ''
123+
experimental-features = nix-command flakes
124+
'';
125+
126+
etcNixConf = runCommand "etcd-nix-conf" { } ''
127+
mkdir -p $out/etc/nix/
128+
ln -s ${nixConfFile} $out/etc/nix/nix.conf
129+
'';
130+
112131
# https://github.com/NixOS/nix/blob/2.8.0/src/libstore/globals.hh#L464-L465
113132
sandboxBuildDir = "/build";
114133

@@ -142,6 +161,8 @@ let
142161
# TODO: Make configurable?
143162
NIX_BUILD_CORES = "1";
144163

164+
# Make sure we get the libraries for C and C++ in.
165+
LD_LIBRARY_PATH = lib.makeLibraryPath [ stdenv.cc.cc ];
145166
}
146167
// drvEnv
147168
// {
@@ -153,10 +174,10 @@ let
153174
TMPDIR = sandboxBuildDir;
154175
TEMPDIR = sandboxBuildDir;
155176
TMP = sandboxBuildDir;
156-
TEMP = sandboxBuildDir;
177+
TEMP = "/tmp";
157178

158179
# https://github.com/NixOS/nix/blob/2.8.0/src/libstore/build/local-derivation-goal.cc#L1015-L1019
159-
PWD = sandboxBuildDir;
180+
PWD = homeDirectory;
160181

161182
# https://github.com/NixOS/nix/blob/2.8.0/src/libstore/build/local-derivation-goal.cc#L1071-L1074
162183
# We don't set it here because the output here isn't handled in any special way
@@ -172,16 +193,17 @@ let
172193
contents = [
173194
binSh
174195
usrBinEnv
196+
etcNixConf
175197
(fakeNss.override {
176198
# Allows programs to look up the build user's home directory
177199
# https://github.com/NixOS/nix/blob/ffe155abd36366a870482625543f9bf924a58281/src/libstore/build/local-derivation-goal.cc#L906-L910
178200
# Slightly differs however: We use the passed-in homeDirectory instead of sandboxBuildDir.
179201
# We're doing this because it's arguably a bug in Nix that sandboxBuildDir is used here: https://github.com/NixOS/nix/issues/6379
180202
extraPasswdLines = [
181-
"nixbld:x:${toString uid}:${toString gid}:Build user:${homeDirectory}:/noshell"
203+
"${toString uname}:x:${toString uid}:${toString gid}:Build user:${homeDirectory}:${lib.escapeShellArg shell}"
182204
];
183205
extraGroupLines = [
184-
"nixbld:!:${toString gid}:"
206+
"${toString uname}:!:${toString gid}:"
185207
];
186208
})
187209
];
@@ -197,6 +219,28 @@ let
197219
# Gives the user control over the build directory
198220
mkdir -p .${sandboxBuildDir}
199221
chown -R ${toString uid}:${toString gid} .${sandboxBuildDir}
222+
223+
mkdir -p .${homeDirectory}
224+
chown -R ${toString uid}:${toString gid} .${homeDirectory}
225+
226+
mkdir -p ./tmp
227+
chown -R ${toString uid}:${toString gid} ./tmp
228+
229+
mkdir -p ./etc/skel
230+
chown -R ${toString uid}:${toString gid} ./etc/skel
231+
232+
# Create traditional /lib or /lib64 as needed.
233+
# For aarch64 (arm64):
234+
if [ -e "${glibc}/lib/ld-linux-aarch64.so.1" ]; then
235+
mkdir -p ./lib
236+
ln -s "${glibc}/lib/ld-linux-aarch64.so.1" ./lib/ld-linux-aarch64.so.1
237+
fi
238+
239+
# For x86_64:
240+
if [ -e "${glibc}/lib64/ld-linux-x86-64.so.2" ]; then
241+
mkdir -p ./lib64
242+
ln -s "${glibc}/lib64/ld-linux-x86-64.so.2" ./lib64/ld-linux-x86-64.so.2
243+
fi
200244
'';
201245

202246
# Run this image as the given uid/gid
@@ -215,11 +259,12 @@ let
215259
shell
216260
rcfile
217261
];
218-
config.WorkingDir = sandboxBuildDir;
262+
config.WorkingDir = homeDirectory;
219263
config.Env = lib.mapAttrsToList (name: value: "${name}=${value}") envVars;
220264
};
221265
in
222266
{
267+
inherit streamNixShellImage;
223268

224269
# This function streams a docker image that behaves like a nix-shell for a derivation
225270
# Docs: doc/build-helpers/images/dockertools.section.md

0 commit comments

Comments
 (0)