Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions nix-shebang.el
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
;; This detects file headers that look like:
;; #!/usr/bin/env nix-shell
;; #!nix-shell -i bash
;; As well as:
;; #!/usr/bin/env nix
;; #! nix shell nixpkgs#bash nixpkgs#hello nixpkgs#cowsay --command bash

;; and correctly detects their file modes.

Expand All @@ -20,14 +23,19 @@

(defvar nix-shebang-interpreter-regexp "#!\s*nix-shell -i \\([^ \t\n]+\\)"
"Regexp for nix-shell -i header.")
(defvar nix-shebang-flake-interpreter-regexp "^#!\s*nix .*--command \\(.*\\)"
"Regexp for nix shell script using flakes.")

(defun nix-shebang-get-interpreter ()
"Get interpreter string from nix-shell -i file."
"Get interpreter string"
(save-excursion
(goto-char (point-min))
(forward-line 1)
(when (looking-at nix-shebang-interpreter-regexp)
(match-string 1))))
(forward-line 1)
(if (looking-at nix-shebang-interpreter-regexp)
(match-string 1)
(if (and (re-search-forward nix-shebang-flake-interpreter-regexp nil t)
(looking-back nix-shebang-flake-interpreter-regexp))
(match-string 1)))))

(defun nix-shebang-mode ()
"Detect and run file’s interpreter mode."
Expand Down