Skip to content

Commit e4cc6e6

Browse files
committed
Add package lockedClojure that will run clojure and clj in locked environments - mainly for shells
- $HOME stays writeable - Update example to show usage, and how it is different from `shellEnv` - Document more in `example/README.md`
1 parent 7fe6de4 commit e4cc6e6

File tree

4 files changed

+56
-8
lines changed

4 files changed

+56
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ You can generate a flake example with:
2626
mkdir play-with-clojure-nix-locker && cd play-with-clojure-nix-locker && nix flake init -t github:bevuta/clojure-nix-locker
2727
```
2828

29-
The [example README](example/README.md) has some next steps.
29+
The [example README](example/README.md) has some next steps and further documentation.
3030

3131
## Why another tool?
3232

example/README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,32 @@ Run uberjar:
2626
nix develop
2727
```
2828

29-
It will print out the current locked classpath.
29+
It will print out the current locked classpath.
30+
31+
# Two ways to get a locked classpath
32+
33+
## Sourcing `shellEnv`
34+
35+
During a `buildPhase` you can source the locked `shellEnv` like this:
36+
37+
```sh
38+
source ${my-clojure-nix-locker.shellEnv}
39+
```
40+
41+
This overrides `$JAVA_TOOL_OPTIONS` and `$HOME` to the locked classpath. Great for building, not great for devShells.
42+
43+
## Using `lockedClojure`
44+
45+
`lockedClojure` wraps `pkgs.clojure`, overriding `$JAVA_TOOL_OPTIONS` and `$HOME` only on `clojure` or `clj` invocation. Great for devShells.
46+
47+
If `pkgs.clojure` is anywhere in the set of inputs for a devShell, it may override the `lockedClojure`. Check with:
48+
49+
```sh
50+
clojure -Spath
51+
```
52+
53+
You should see a classpath with references to `/nix/store`.
54+
55+
# Overriding other programs that are aware of classpaths
56+
57+
`wrapPrograms` is available to wrap other programs into the locked classpath. `wrapClojure` is also available if you want to wrap a custom `pkgs.clojure`.

example/flake.nix

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
];
3535

3636
buildPhase = ''
37+
# Overrides $HOME as well. Great for building, not great for devShells
3738
source ${my-clojure-nix-locker.shellEnv}
3839
3940
# Now compile as in https://clojure.org/guides/tools_build#_compiled_uberjar_application_build
@@ -58,30 +59,42 @@
5859
# Trace all Bash executions
5960
set -o xtrace
6061
61-
source ${my-clojure-nix-locker.shellEnv}
62-
6362
echo "Current locked classpath:"
64-
${pkgs.clojure}/bin/clojure -Spath
63+
64+
# This will work, but, $HOME will be read-only which is probably not what you want in a devShell
65+
#source ${my-clojure-nix-locker.shellEnv}
66+
#${pkgs.clojure}/bin/clojure -Spath
67+
68+
# Using the provided lockedClojure will do what you want
69+
# And if pkgs.clojure is referenced anywhere, it may override it
70+
${my-clojure-nix-locker.lockedClojure}/bin/clojure -Spath
6571
6672
set +o xtrace
6773
6874
echo
69-
echo "Note that \$HOME is overridden and read-only: $HOME"
75+
echo "Note that \$HOME will be overriden if you sourced my-clojure-nix-locker.shellEnv: $HOME"
76+
echo "If you used my-clojure-nix-locker.lockedClojure, it will be left alone and only the clojure and clj commands are overridden and locked"
77+
echo
78+
echo "This command should be locked in this shell:"
79+
echo "clojure -Spath"
7080
echo
7181
'';
7282
inputsFrom = [
73-
packages.uberjar
83+
# Will pull in pkgs.clojure, and we want my-clojure-nix-locker.lockedClojure
84+
#packages.uberjar
7485
];
7586
buildInputs = with pkgs; [
7687
openjdk
7788
cacert # for maven and tools.gitlibs
78-
clojure
7989
clj-kondo
8090
coreutils
8191
# This provides the standalone `clojure-nix-locker` script in the shell
8292
# You can use it, or `nix run .#locker`
8393
# Both does the same
8494
my-clojure-nix-locker.locker
95+
# Use the locked clojure
96+
# A pkgs.clojure reference could override it
97+
my-clojure-nix-locker.lockedClojure
8598
];
8699
};
87100
});

flake.nix

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030
locker = locked.commandLocker command;
3131
homeDirectory = locked.homeDirectory;
3232
shellEnv = locked.shellEnv;
33+
# Function to wrap your own overridden pkgs.clojure into a locked environment, a special case
34+
wrapClojure = locked.wrapClojure;
35+
# Provide an already locked clojure
36+
# You want to ensure that pkgs.clojure are not reference anywhere else
37+
lockedClojure = locked.wrapClojure pkgs.clojure;
38+
# Function to wrap other Java classpath aware programs with the locked environment
39+
wrapPrograms = locked.wrapPrograms;
3340
};
3441
};
3542

0 commit comments

Comments
 (0)