Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1# Functions to build elisp files to locally configure emcas buffers.
2# See https://github.com/shlevy/nix-buffer
3
4{ lib, writeText, inherit-local }:
5
6rec {
7 withPackages = pkgs': let
8 pkgs = builtins.filter (x: x != null) pkgs';
9 extras = map (x: x.emacsBufferSetup pkgs) (builtins.filter (builtins.hasAttr "emacsBufferSetup") pkgs);
10 in writeText "dir-locals.el" ''
11 (require 'inherit-local "${inherit-local}/share/emacs/site-lisp/elpa/inherit-local-${inherit-local.version}/inherit-local.elc")
12
13 ; Only set up nixpkgs buffer handling when we have some buffers active
14 (defvar nixpkgs--buffer-count 0)
15 (when (eq nixpkgs--buffer-count 0)
16 (make-variable-buffer-local 'nixpkgs--is-nixpkgs-buffer)
17 ; When generating a new temporary buffer (one whose name starts with a space), do inherit-local inheritance and make it a nixpkgs buffer
18 (defun nixpkgs--around-generate (orig name)
19 (if (and nixpkgs--is-nixpkgs-buffer (eq (aref name 0) ?\s))
20 (let ((buf (funcall orig name)))
21 (progn
22 (inherit-local-inherit-child buf)
23 (with-current-buffer buf
24 (setq nixpkgs--buffer-count (1+ nixpkgs--buffer-count))
25 (add-hook 'kill-buffer-hook 'nixpkgs--decrement-buffer-count nil t)))
26 buf)
27 (funcall orig name)))
28 (advice-add 'generate-new-buffer :around #'nixpkgs--around-generate)
29 ; When we have no more nixpkgs buffers, tear down the buffer handling
30 (defun nixpkgs--decrement-buffer-count ()
31 (setq nixpkgs--buffer-count (1- nixpkgs--buffer-count))
32 (when (eq nixpkgs--buffer-count 0)
33 (advice-remove 'generate-new-buffer #'nixpkgs--around-generate)
34 (fmakunbound 'nixpkgs--around-generate)
35 (fmakunbound 'nixpkgs--decrement-buffer-count))))
36 (setq nixpkgs--buffer-count (1+ nixpkgs--buffer-count))
37 (add-hook 'kill-buffer-hook 'nixpkgs--decrement-buffer-count nil t)
38
39 ; Add packages to PATH and exec-path
40 (make-local-variable 'process-environment)
41 (put 'process-environment 'permanent-local t)
42 (inherit-local 'process-environment)
43 ; setenv modifies in place, so copy the environment first
44 (setq process-environment (copy-tree process-environment))
45 (setenv "PATH" (concat "${lib.makeSearchPath "bin" pkgs}:" (getenv "PATH")))
46 (inherit-local-permanent exec-path (append '(${builtins.concatStringsSep " " (map (p: "\"${p}/bin\"") pkgs)}) exec-path))
47
48 (inherit-local-permanent eshell-path-env (concat "${lib.makeSearchPath "bin" pkgs}:" eshell-path-env))
49
50 (setq nixpkgs--is-nixpkgs-buffer t)
51 (inherit-local 'nixpkgs--is-nixpkgs-buffer)
52
53 ${lib.concatStringsSep "\n" extras}
54 '';
55 # nix-buffer function for a project with a bunch of haskell packages
56 # in one directory
57 haskellMonoRepo = { project-root # The monorepo root
58 , haskellPackages # The composed haskell packages set that contains all of the packages
59 }: { root }:
60 let # The haskell paths.
61 haskell-paths = lib.filesystem.haskellPathsInDir project-root;
62 # Find the haskell package that the 'root' is in, if any.
63 haskell-path-parent =
64 let filtered = builtins.filter (name:
65 lib.hasPrefix (toString (project-root + "/${name}")) (toString root)
66 ) (builtins.attrNames haskell-paths);
67 in
68 if filtered == [] then null else builtins.head filtered;
69 # We're in the directory of a haskell package
70 is-haskell-package = haskell-path-parent != null;
71 haskell-package = haskellPackages.${haskell-path-parent};
72 # GHC environment with all needed deps for the haskell package
73 haskell-package-env =
74 builtins.head haskell-package.env.nativeBuildInputs;
75 in
76 if is-haskell-package
77 then withPackages [ haskell-package-env ]
78 else {};
79}