Skip to content

Commit fc900c1

Browse files
authored
Support more recent VSCode Server versions (#78)
* fix uses of ~ that should be $HOME * Support for new vscode server directory structure * Be backwards compatible with previous versions of nixos-vscode-server by copying the patch marker files
1 parent d0ed9b8 commit fc900c1

File tree

3 files changed

+59
-38
lines changed

3 files changed

+59
-38
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ The installation path for VS Code server is configurable and the default can dif
144144

145145
```nix
146146
{
147-
services.vscode-server.installPath = "~/.vscode-server-oss";
147+
services.vscode-server.installPath = "$HOME/.vscode-server-oss";
148148
}
149149
```
150150

modules/vscode-server/module.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ moduleConfig: {
3434

3535
installPath = mkOption {
3636
type = str;
37-
default = "~/.vscode-server";
38-
example = "~/.vscode-server-oss";
37+
default = "$HOME/.vscode-server";
38+
example = "$HOME/.vscode-server-oss";
3939
description = ''
4040
The install path.
4141
'';

pkgs/auto-fix-vscode-server.nix

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
enableFHS ? false,
2222
nodejsPackage ? null,
2323
extraRuntimeDependencies ? [ ],
24-
installPath ? "~/.vscode-server",
24+
installPath ? "$HOME/.vscode-server",
2525
postPatch ? "",
2626
}: let
2727
inherit (lib) makeBinPath makeLibraryPath optionalString;
@@ -71,10 +71,8 @@
7171
name = "patchelf-vscode-server";
7272
runtimeInputs = [ coreutils findutils patchelf ];
7373
text = ''
74-
bin=$1
75-
bin_dir=${installPath}/bin/$bin
76-
patched_file=${installPath}/.$bin.patched
77-
orig_node=${installPath}/.$bin.node
74+
bin_dir="$1"
75+
patched_file="$bin_dir/.nixos-patched"
7876
7977
# NOTE: We don't log here because it won't show up in the output of the user service.
8078
@@ -109,7 +107,6 @@
109107
patchelf --shrink-rpath "$elf"
110108
}
111109
112-
patch_elf "$orig_node"
113110
while read -rd ''' elf; do
114111
patch_elf "$elf"
115112
done < <(find "$bin_dir" -type f -perm -100 -printf '%p\0')
@@ -126,31 +123,41 @@
126123
name = "auto-fix-vscode-server";
127124
runtimeInputs = [ coreutils findutils inotify-tools ];
128125
text = ''
129-
bins_dir=${installPath}/bin
126+
bins_dir_1=${installPath}/bin
127+
bins_dir_2=${installPath}/cli/servers
130128
131129
patch_bin () {
132-
local bin=$1
133-
bin=''${bin:0:40}
134-
local actual_dir=$bins_dir/$1
135-
local patched_file=${installPath}/.$bin.patched
130+
local actual_dir="$1"
131+
local patched_file="$actual_dir/.nixos-patched"
136132
137133
if [[ -e $patched_file ]]; then
138134
return 0
139135
fi
140136
137+
# Backwards compatibility with previous versions of nixos-vscode-server.
138+
local old_patched_file
139+
old_patched_file="$(basename "$actual_dir")"
140+
if [[ $old_patched_file == "server" ]]; then
141+
old_patched_file="$(basename "$(dirname "$actual_dir")")"
142+
old_patched_file="${installPath}/.''${old_patched_file%%.*}.patched"
143+
else
144+
old_patched_file="${installPath}/.''${old_patched_file%%-*}.patched"
145+
fi
146+
if [[ -e $old_patched_file ]]; then
147+
echo "Migrating old nixos-vscode-server patch marker file to new location in $actual_dir." >&2
148+
cp "$old_patched_file" "$patched_file"
149+
return 0
150+
fi
151+
141152
echo "Patching Node.js of VS Code server installation in $actual_dir..." >&2
142153
143-
${optionalString (nodejs != null) ''
144-
ln -sfT ${
145-
if enableFHS
146-
then nodejsFHS
147-
else nodejs
148-
}/bin/node "$actual_dir/node"
154+
mv "$actual_dir/node" "$actual_dir/node.patched"
155+
156+
${optionalString (enableFHS) ''
157+
ln -sfT ${nodejsFHS}/bin/node "$actual_dir/node"
149158
''}
150159
151160
${optionalString (!enableFHS || postPatch != "") ''
152-
local orig_node=${installPath}/.$bin.node
153-
mv "$actual_dir/node" "$orig_node"
154161
cat <<EOF > "$actual_dir/node"
155162
#!${runtimeShell}
156163
@@ -159,10 +166,15 @@
159166
160167
# We leave the rest up to the Bash script
161168
# to keep having to deal with 'sh' compatibility to a minimum.
162-
${patchELFScript}/bin/patchelf-vscode-server '$bin'
169+
${patchELFScript}/bin/patchelf-vscode-server \$(dirname "\$0")
163170
164171
# Let Node.js take over as if this script never existed.
165-
exec '$orig_node' "\$@"
172+
${
173+
let nodePath = (if (nodejs != null)
174+
then "${if enableFHS then nodejsFHS else nodejs}/bin/node"
175+
else ''\$(dirname "\$0")/node.patched'');
176+
in ''exec "${nodePath}" "\$@"''
177+
}
166178
EOF
167179
chmod +x "$actual_dir/node"
168180
''}
@@ -171,30 +183,39 @@
171183
echo 0 > "$patched_file"
172184
}
173185
174-
# Fix any existing symlinks before we enter the inotify loop.
175-
if [[ -e $bins_dir ]]; then
176-
while read -rd ''' bin; do
177-
patch_bin "$bin"
178-
done < <(find "$bins_dir" -mindepth 1 -maxdepth 1 -type d -printf '%P\0')
179-
else
180-
mkdir -p "$bins_dir"
181-
fi
182-
183-
while IFS=: read -r bin event; do
186+
mkdir -p "$bins_dir_1" "$bins_dir_2"
187+
while read -rd ''' bin; do
188+
if [[ $bin == "$bins_dir_2"* ]]; then
189+
bin="$bin/server"
190+
fi
191+
patch_bin "$bin"
192+
done < <(find "$bins_dir_1" "$bins_dir_2" -mindepth 1 -maxdepth 1 -type d -printf '%p\0')
193+
194+
while IFS=: read -r bins_dir bin event; do
184195
# A new version of the VS Code Server is being created.
185196
if [[ $event == 'CREATE,ISDIR' ]]; then
186-
actual_dir=$bins_dir/$bin
197+
actual_dir="$bins_dir$bin"
198+
if [[ "$bins_dir" == "$bins_dir_2/" ]]; then
199+
actual_dir="$actual_dir/server"
200+
# Hope that VSCode will not die if the directory exists when it tries to install, otherwise we'll need to
201+
# use a coproc to wait for the directory to be created without entering in a race, then watch for the node
202+
# file to be created (probably while also avoiding a race)
203+
# https://unix.stackexchange.com/a/185370
204+
mkdir -p "$actual_dir"
205+
fi
187206
echo "VS Code server is being installed in $actual_dir..." >&2
207+
# Quickly create a node file, which will be removed when vscode installs its own version
188208
touch "$actual_dir/node"
209+
# Hope we don't race...
189210
inotifywait -qq -e DELETE_SELF "$actual_dir/node"
190-
patch_bin "$bin"
211+
patch_bin "$actual_dir"
191212
# The monitored directory is deleted, e.g. when "Uninstall VS Code Server from Host" has been run.
192213
elif [[ $event == DELETE_SELF ]]; then
193214
# See the comments above Restart in the service config.
194215
exit 0
195216
fi
196-
done < <(inotifywait -q -m -e CREATE,ISDIR -e DELETE_SELF --format '%f:%e' "$bins_dir")
217+
done < <(inotifywait -q -m -e CREATE,ISDIR -e DELETE_SELF --format '%w:%f:%e' "$bins_dir_1" "$bins_dir_2")
197218
'';
198219
};
199220
in
200-
autoFixScript
221+
autoFixScript

0 commit comments

Comments
 (0)