Skip to content

Commit 40abcaa

Browse files
Implement support for flake.nix: nix develop
1 parent e7bf92d commit 40abcaa

File tree

7 files changed

+66
-20
lines changed

7 files changed

+66
-20
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Hi folks,
33
My name is Roman Valihura. I'm the author of this extension. I'm Ukrainian.
44
I was born in Ukraine. I'm living here at the moment.
55

6-
As you all know Russia invaded my country.
6+
As you all know Russia invaded my country.
77
Russia has already killed thousands of civilians and continues the war and terror in Ukraine.
88
I have the luck that my region is pretty far from the frontline. But even here, I'm living in the air-alarm reality.
99
The reality where you should wake up in the middle of the night and go into the shelter. Because a rocket flies over your region.
@@ -49,7 +49,7 @@ However, this process can quickly become tedious.
4949
- Install [Nix package manager](https://nixos.org/nix/).
5050
- Restart VS Code (to make sure that `nix-shell` is in the PATH)
5151
- [Install the extension](https://marketplace.visualstudio.com/items?itemName=arrterian.nix-env-selector).
52-
- Create the Nix environment config (like `default.nix` or `shell.nix`) in
52+
- Create the Nix environment config (like `default.nix` or `shell.nix` or `flake.nix`) in
5353
the root of your project's workspace.
5454
- Open Command Palette (<kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>P</kbd>)
5555
and run `Nix-Env: Select Environment` command.
@@ -111,6 +111,8 @@ file (located in the root of the workspace). Here are the configuration settings
111111
| `nixEnvSelector.packages` | [] | List packages using as `-p` nix-shell args |
112112
| `nixEnvSelector.args` | null | Custom args string for nix-shell. EX: `-A <something> --pure` |
113113
| `nixEnvSelector.nixShellPath` | null | Custom path for nix-shell executable |
114+
| `nixEnvSelector.useFlakes` | false | Enable support for `flake.nix` |
115+
114116

115117
## Supported Platforms
116118

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "nix-env-selector",
33
"displayName": "Nix Environment Selector",
44
"description": "Allows switch environment for Visual Studio Code and extensions based on Nix config file.",
5-
"version": "1.0.12",
5+
"version": "1.1.0",
66
"keywords": [
77
"nix",
88
"nix-env",
@@ -69,7 +69,12 @@
6969
"type": "string",
7070
"deprecationMessage": "[DEPRECATED] Use 'args' instead",
7171
"description": "Attribute path (nix-shell -A)"
72-
}
72+
},
73+
"nixEnvSelector.useFlakes": {
74+
"type": "boolean",
75+
"default": false,
76+
"description": "Enable support for Nix flakes"
77+
}
7378
}
7479
},
7580
"commands": [

src/main/config.cljs

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
:nix-args (-> (workspace/config-get vscode-config :nix-env-selector/args)
1717
(#(when %1 (render-workspace %1 workspace-root))))
1818
:nix-shell-path (-> (workspace/config-get vscode-config :nix-env-selector/nix-shell-path)
19-
(#(when %1 (render-workspace %1 workspace-root))))})))
19+
(#(when %1 (render-workspace %1 workspace-root))))
20+
:use-flakes (workspace/config-get vscode-config :nix-env-selector/use-flakes)})))
2021

2122
(workspace/on-config-change update-config!)

src/main/ext/actions.cljs

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
status)
4848
(->> (env/get-nix-env-async {:nix-config nix-path
4949
:args (:nix-args @config)
50-
:nix-shell-path (:nix-shell-path @config)}
50+
:nix-shell-path (:nix-shell-path @config)
51+
:use-flakes (:use-flakes @config)}
5152
log-channel)
5253
(p/map (fn [env-vars]
5354
(when env-vars

src/main/ext/nix_env.cljs

+45-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
(defn ^:private list-to-args [pref-arg list]
99
(s/join " " (map #(str pref-arg " " %1) list)))
1010

11+
(defn ^:private has-flake? [dir]
12+
(let [fs (js/require "fs")]
13+
(try
14+
(.existsSync fs (str dir "/flake.nix"))
15+
(catch js/Error _
16+
false))))
17+
1118
(defn ^:private get-shell-env-cmd [{:keys [packages
1219
nix-shell-path
1320
nix-config
@@ -29,6 +36,17 @@
2936
(when args
3037
(str " " args))))
3138

39+
(defn ^:private get-flake-env-cmd [{:keys [nix-path dir args]}]
40+
(str (if (empty? nix-path)
41+
"nix"
42+
(s/replace nix-path #" " "\\ "))
43+
" develop "
44+
(when dir
45+
(str "\"" dir "\""))
46+
" --command env"
47+
(when args
48+
(str " " args))))
49+
3250
(defn ^:private parse-exported-vars [output]
3351
(->> (s/split output #"declare -x")
3452
(filter not-empty)
@@ -39,25 +57,43 @@
3957
(catch js/Error _ nil))]))))
4058
(filter not-empty)))
4159

42-
(defn get-nix-env-sync [options log-channel]
43-
(let [cmd (get-shell-env-cmd options)]
60+
(defn ^:private parse-env-vars [output]
61+
(->> (s/split output #"\n")
62+
(filter not-empty)
63+
(map #(s/split %1 #"=" 2))
64+
(filter #(= (count %) 2))
65+
(map (fn [[name value]]
66+
[name value]))))
67+
68+
(defn get-nix-env-sync [{:keys [use-flakes] :as options} log-channel]
69+
(let [dir (dirname (:nix-config options))
70+
is-flake (and use-flakes (has-flake? dir))
71+
cmd (if is-flake
72+
(get-flake-env-cmd (assoc options :dir dir))
73+
(get-shell-env-cmd options))
74+
parser (if is-flake parse-env-vars parse-exported-vars)]
4475
(w/write-log log-channel (str "Running command synchronously: " cmd))
45-
(-> (execSync (clj->js cmd {:cwd (dirname (:nix-config options))}))
76+
(-> (execSync (clj->js cmd {:cwd dir}))
4677
(.toString)
47-
(parse-exported-vars))))
78+
(parser))))
4879

49-
(defn get-nix-env-async [options log-channel]
80+
(defn get-nix-env-async [{:keys [use-flakes] :as options} log-channel]
5081
(let [env-result (p/deferred)
51-
cmd (get-shell-env-cmd options)]
52-
(w/write-log log-channel (str "Running command asynchronously: " cmd))
53-
(exec (clj->js cmd {:cwd (dirname (:nix-config options))})
82+
dir (dirname (:nix-config options))
83+
is-flake (and use-flakes (has-flake? dir))
84+
cmd (if is-flake
85+
(get-flake-env-cmd (assoc options :dir dir))
86+
(get-shell-env-cmd options))
87+
parser (if is-flake parse-env-vars parse-exported-vars)]
88+
(w/write-log log-channel (str "Running command " (if is-flake "with flake" "with nix-shell") ": " cmd))
89+
(exec (clj->js cmd {:cwd dir})
5490
(fn [err result stderr]
5591
(if (nil? err)
5692
(p/resolve! env-result result)
5793
(do
5894
(w/write-log log-channel (str "Error applying environment: " stderr))
5995
(p/reject! env-result err)))))
60-
(p/map parse-exported-vars env-result)))
96+
(p/map parser env-result)))
6197

6298
(defn set-current-env [env-vars]
6399
(mapv (fn [[name value]]

src/main/main.cljs

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
(if (or (not-empty (:nix-file @config)) (not-empty (:nix-packages @config)))
2020
(try
2121
(-> (env/get-nix-env-sync {:nix-config (:nix-file @config)
22-
:packages (:nix-packages @config)
23-
:args (:nix-args @config)
24-
:nix-shell-path (:nix-shell-path @config)}
22+
:packages (:nix-packages @config)
23+
:args (:nix-args @config)
24+
:nix-shell-path (:nix-shell-path @config)
25+
:use-flakes (:use-flakes @config)}
2526
log-channel)
2627
(env/set-current-env))
2728
(->> status-bar

0 commit comments

Comments
 (0)