Skip to content

Commit 2eca6dd

Browse files
committed
fix(nix): persist config files against nix-collect-garbage
1 parent 4abdcc9 commit 2eca6dd

File tree

1 file changed

+71
-46
lines changed

1 file changed

+71
-46
lines changed

package.nix

Lines changed: 71 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,84 @@
11
{
22
stdenv,
33
lib,
4+
pkgs,
45
writeShellApplication,
56
tree-sitter-nu ? fetchGit "https://github.com/nushell/tree-sitter-nu",
67
topiary,
78
nushell,
89
writeText,
910
callPackage,
1011
}:
11-
writeShellApplication (let
12-
libtree-sitter-nu = callPackage ({
13-
lib,
14-
stdenv,
15-
}:
16-
stdenv.mkDerivation (finalAttrs: {
17-
pname = "tree-sitter-nu";
18-
version = tree-sitter-nu.rev;
19-
20-
src = tree-sitter-nu;
21-
22-
makeFlags = [
23-
# The PREFIX var isn't picking up from stdenv.
24-
"PREFIX=$(out)"
25-
];
26-
27-
meta = with lib; {
28-
description = "A tree-sitter grammar for nu-lang, the language of nushell";
29-
homepage = "https://github.com/nushell/tree-sitter-nu";
30-
license = licenses.mit;
31-
};
32-
})) {};
33-
in {
34-
name = "topiary-nushell";
35-
runtimeInputs = [nushell topiary];
36-
runtimeEnv = let
37-
extension = with stdenv;
38-
if isLinux
39-
then ".so"
40-
else if isDarwin
41-
then ".dylib"
42-
else throw "Unsupported system: ${system}";
43-
in {
44-
TOPIARY_CONFIG_FILE = writeText "languages.ncl" ''
12+
writeShellApplication (
13+
let
14+
libtree-sitter-nu = callPackage (
4515
{
46-
languages = {
47-
nu = {
48-
extensions = ["nu"],
49-
grammar.source.path = "${libtree-sitter-nu}/lib/libtree-sitter-nu${extension}",
16+
lib,
17+
stdenv,
18+
}:
19+
stdenv.mkDerivation (finalAttrs: {
20+
pname = "tree-sitter-nu";
21+
version = tree-sitter-nu.rev;
22+
23+
src = tree-sitter-nu;
24+
25+
makeFlags = [
26+
# The PREFIX var isn't picking up from stdenv.
27+
"PREFIX=$(out)"
28+
];
29+
30+
meta = with lib; {
31+
description = "A tree-sitter grammar for nu-lang, the language of nushell";
32+
homepage = "https://github.com/nushell/tree-sitter-nu";
33+
license = licenses.mit;
34+
};
35+
})
36+
) { };
37+
38+
extension =
39+
with stdenv;
40+
if isLinux then
41+
".so"
42+
else if isDarwin then
43+
".dylib"
44+
else
45+
throw "Unsupported system: ${system}";
46+
47+
# Create a directory holding ALL runtime config files
48+
# This makes a single path for GC root.
49+
topiaryConfigDir = pkgs.runCommand "topiary-nushell-config" { } ''
50+
local_config_dir="$out"
51+
52+
mkdir -p $local_config_dir/languages
53+
54+
# 1. Copy the nu.scm language directory
55+
cp -r ${./languages/nu.scm} $local_config_dir/languages/nu.scm
56+
57+
# 2. Write the languages.ncl file, referencing the compiled grammar
58+
cat ${writeText "languages.ncl" ''
59+
{
60+
languages = {
61+
nu = {
62+
extensions = ["nu"],
63+
grammar.source.path = "${libtree-sitter-nu}/lib/libtree-sitter-nu${extension}",
64+
},
5065
},
51-
},
52-
}
66+
}
67+
''} > $local_config_dir/languages.ncl
68+
'';
69+
in
70+
{
71+
name = "topiary-nushell";
72+
runtimeInputs = [
73+
nushell
74+
topiary
75+
];
76+
runtimeEnv = {
77+
TOPIARY_CONFIG_FILE = "${topiaryConfigDir}/languages.ncl";
78+
TOPIARY_LANGUAGE_DIR = "${topiaryConfigDir}/languages";
79+
};
80+
text = ''
81+
${lib.getExe topiary} "$@"
5382
'';
54-
TOPIARY_LANGUAGE_DIR = ./languages;
55-
};
56-
text = ''
57-
${lib.getExe topiary} "$@"
58-
'';
59-
})
83+
}
84+
)

0 commit comments

Comments
 (0)