[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/nix-mode ba202b6657 220/500: Add nix-shell.el
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/nix-mode ba202b6657 220/500: Add nix-shell.el |
Date: |
Sat, 29 Jan 2022 08:27:08 -0500 (EST) |
branch: elpa/nix-mode
commit ba202b66578cdaad3aac8c7c0be2cda3760b40be
Author: Matthew Bauer <matthew.bauer@obsidian.systems>
Commit: Matthew Bauer <matthew.bauer@obsidian.systems>
Add nix-shell.el
---
nix-shell.el | 193 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
nix.el | 20 +------
2 files changed, 194 insertions(+), 19 deletions(-)
diff --git a/nix-shell.el b/nix-shell.el
new file mode 100644
index 0000000000..2f6bba85ab
--- /dev/null
+++ b/nix-shell.el
@@ -0,0 +1,193 @@
+;;; nix-shell.el -- run nix commands in Emacs -*- lexical-binding: t -*-
+
+;; Author: Matthew Bauer <mjbauer95@gmail.com>
+;; Homepage: https://github.com/NixOS/nix-mode
+;; Keywords: nix
+
+;; This file is NOT part of GNU Emacs.
+
+;;; Commentary:
+
+;; To use this just run:
+
+;; M-x RET nix-shell RET
+
+;; This will give you some
+
+;;; Code:
+
+(require 'nix)
+(require 'nix-instantiate)
+
+(defcustom nix-shell-file nil
+ "Set to the file to run the nix-shell for."
+ :type 'string
+ :group 'nix)
+
+(defcustom nix-shell-inputs '(depsBuildBuild
+ depsBuildBuildPropagated
+ nativeBuildInputs
+ propagatedNativeBuildInputs
+ depsBuildTarget
+ depsBuildTargetPropagated)
+ "List of inputs to collect for nix-shell."
+ :type 'list
+ :group 'nix)
+
+(defcustom nix-shell-clear-environment nil
+ "Whether to clear the old ‘exec-path’ & environment.
+Similar to ‘--pure’ argument in command line nix-shell."
+ :type 'boolean
+ :group 'nix)
+
+(defun nix-shell--callback (buffer drv)
+ "Run the nix-shell callback to setup the buffer.
+The BUFFER to run in.
+The DRV file to use."
+ (let* ((env (alist-get 'env drv))
+ (stdenv (alist-get 'stdenv env))
+ (system (alist-get 'system env))
+ (inputs (remove nil
+ (apply 'append
+ (mapcar (lambda (prop)
+ (split-string (alist-get prop env)))
+ nix-shell-inputs)))))
+
+ ;; Prevent accidentally rebuilding the world.
+ (unless (file-directory-p stdenv)
+ (error
+ "Your stdenv at %s has not been built. Please run: nix-store -r %s"
+ stdenv stdenv))
+
+ ;; Make sure this .drv file can actually be built here.
+ (unless (string= system (nix-system))
+ (error
+ "Your system (%s) does not match the target system (%s)"
+ (nix-system) system))
+
+ (with-current-buffer buffer
+ (when nix-shell-clear-environment
+ (setq-local exec-path nil)
+ (setq-local eshell-path-env "")
+ ;; (setq-local process-environment nil)
+ )
+
+ (dolist (input inputs)
+ (unless (file-directory-p input) (nix-store-realise input))
+
+ (let ((bin (expand-file-name "bin" input))
+ (man (expand-file-name "share/man" input))
+ (include (expand-file-name "include" input)))
+ (add-to-list 'exec-path bin)
+ (setq-local eshell-path-env
+ (format "%s:%s" bin eshell-path-env))
+ (add-to-list 'woman-manpath man)
+ (add-to-list 'ffap-c-path include)
+ (add-to-list 'Man-header-file-path include)
+ (add-to-list 'irony-additional-clang-options
+ (format "-I%s" include))))
+
+ (when flycheck-mode
+ (flycheck-buffer))
+ )))
+
+;;;###autoload
+(defun nix-shell-with-packages (packages &optional pkgs-file)
+ "Create a nix shell environment from the listed package.
+PACKAGES a list of packages to use.
+PKGS-FILE the Nix file to get the packages from."
+ (nix-instantiate-async (apply-partially 'nix-shell--callback
+ (current-buffer))
+ (nix-shell--with-packages-file packages pkgs-file)
+ ))
+
+(defun nix-shell--with-packages-file (packages &optional pkgs-file)
+ "Get a .nix file from the packages list.
+PACKAGES to put in the .nix file.
+PKGS-FILE package set to pull from."
+ (unless pkgs-file (setq pkgs-file "<nixpkgs>"))
+ (let ((nix-file (make-temp-file "nix-shell" nil ".nix")))
+ (with-temp-file nix-file
+ (insert (format "with import %s { };\n" pkgs-file))
+ (insert "runCommandCC \"shell\" {\n")
+ (insert " nativeBuildInputs = [\n")
+ (mapcar (lambda (x) (insert (format " %s\n" x))) packages)
+ (insert " ];\n")
+ (insert "} \"\"\n"))
+ nix-file))
+
+(defun nix-eshell-with-packages (packages &optional pkgs-file)
+ "Create an Eshell buffer that has the shell environment in it.
+PACKAGES a list of packages to pull in.
+PKGS-FILE a file to use to get the packages."
+ (let ((buffer (generate-new-buffer "*nix-eshell*")))
+ (pop-to-buffer-same-window buffer)
+
+ (setq-local nix-shell-clear-environment t)
+
+ (nix-shell--callback
+ (current-buffer)
+ (nix-instantiate
+ (nix-shell--with-packages-file packages pkgs-file)))
+
+ (eshell-mode)
+ buffer))
+
+(defun nix-eshell (&optional nix-file attribute)
+ "Create an Eshell buffer that has the shell environment in it.
+NIX-FILE the .nix expression to create a shell for.
+ATTRIBUTE attribute to instantiate in NIX-FILE."
+ (interactive)
+
+ (unless nix-file
+ (when nix-shell-file (setq nix-file nix-shell-file)))
+
+ (unless nix-file (error "Cannot find .nix file"))
+
+ (let ((buffer (generate-new-buffer "*nix-eshell*")))
+ (pop-to-buffer-same-window buffer)
+
+ (setq-local nix-shell-clear-environment t)
+
+ (nix-shell--callback
+ (current-buffer)
+ (nix-instantiate nix-file attribute))
+
+ (eshell-mode)
+ buffer))
+
+;;;###autoload
+(defun nix-shell (&optional nix-file attribute)
+ "A nix-shell emulator in Emacs.
+NIX-FILE the file to instantiate.
+ATTRIBUTE an attribute of the Nix file to use."
+ (interactive)
+
+ (unless nix-file
+ (when nix-shell-file (setq nix-file nix-shell-file)))
+
+ (unless attribute
+ (when nix-shell-attribute (setq attribute nix-shell-attribute)))
+
+ (when nix-file
+ (setq nix-file
+ (expand-file-name nix-file (locate-dominating-file
+ (if (buffer-file-name)
+ (buffer-file-name)
+ default-directory)
+ dir-locals-file)))
+ (nix-instantiate-async (apply-partially 'nix-shell--callback
+ (current-buffer))
+ nix-file attribute)))
+
+;;;###autoload
+(define-minor-mode global-nix-shell-mode
+ "A minor mode for setting up build tools from Nix."
+ :group 'nix
+ :global t
+ (if nix-shell-mode
+ (add-hook 'find-file-hook 'nix-shell t)
+ (remove-hook 'find-file-hook 'nix-shell)))
+
+(provide 'nix-shell)
+;;; nix-shell.el ends here
diff --git a/nix.el b/nix.el
index 365c2c7bbf..c62f4a57f4 100644
--- a/nix.el
+++ b/nix.el
@@ -16,11 +16,9 @@
;;; Code:
-(require 'term)
-
(require 'nix-mode)
(require 'nix-shebang)
-(require 'nix-company)
+(require 'nix-shell)
(defgroup nix nil
"Nix-related customizations"
@@ -72,22 +70,6 @@ DIR is the directory containing the Nix default.nix
expression."
(async-shell-command (format "%s %s -A %s" nix-build-executable dir
attr))
(async-shell-command (format "%s %s" nix-build-executable dir))))
-
-;;;###autoload
-(defun nix-shell (path attribute)
- "Run ‘nix-shell’ in a terminal.
-
-PATH path containing Nix expressions.
-ATTRIBUTE attribute name in nixpkgs to use."
- (interactive
- (list (read-from-minibuffer "Nix path: " "<nixpkgs>")
- (read-from-minibuffer "Nix attribute name: ")))
- (set-buffer (make-term "nix-shell" nix-shell-executable nil
- path "-A" attribute))
- (term-mode)
- (term-char-mode)
- (switch-to-buffer "*nix-shell*"))
-
;;;###autoload
(defun nix-unpack (path attribute)
"Get source from a Nix derivation.
- [nongnu] elpa/nix-mode 05d7c828b3 187/500: Fix nix-company and mmm-mode loading., (continued)
- [nongnu] elpa/nix-mode 05d7c828b3 187/500: Fix nix-company and mmm-mode loading., ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 281bfc1abf 176/500: Cleanup font-locks again, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 407051ad31 175/500: Add font-lock-tests.el file, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode d341ef2f70 195/500: Add missing defcustom type, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode eb7623fb3b 178/500: Handle mmm-mode '' prefixes better, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode cc88a55ead 173/500: Add custom faces for Nix., ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 0181c35a4f 202/500: Add nix-unpack command., ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 0ac0271f6c 201/500: Update Homepage in .el files, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 39f29d8be4 207/500: Replace indentation code with simpler heuristics, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 57ac40d53b 214/500: Merge pull request #45 from eqyiel/master, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode ba202b6657 220/500: Add nix-shell.el,
ELPA Syncer <=
- [nongnu] elpa/nix-mode 082a25ba7d 227/500: Add more default packages in nix.el, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode b98c1083a0 234/500: nix-shell-file not risky, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 9fb4e16c9f 233/500: Setup global-nix-mode, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 161e7ae4d2 237/500: Cleanup nix-instantiate, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode c1b1982ea7 239/500: Fix nix-instantiate.el, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 1b15a6e583 252/500: Merge pull request #46 from matthewbauer/develop, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 1e53bed4d4 299/500: Move the logic for indentation of first line of file, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 79a3715c3d 303/500: Improve handling of 'in' after a hanging 'let'., ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode 6e4f66de1d 317/500: Add all-packages.nix test case, ELPA Syncer, 2022/01/29
- [nongnu] elpa/nix-mode f938aeaeb6 324/500: Fix buggy implementation of nix-smie--anchor., ELPA Syncer, 2022/01/29