2016-09-05 19:01:26 +03:00
|
|
|
# Functions to build elisp files to locally configure emcas buffers.
|
|
|
|
# See https://github.com/shlevy/nix-buffer
|
|
|
|
|
2016-10-07 17:31:37 +03:00
|
|
|
{ lib, writeText, inherit-local }:
|
2016-09-05 19:01:26 +03:00
|
|
|
|
2017-05-01 18:43:53 +03:00
|
|
|
rec {
|
2018-02-12 06:52:16 +03:00
|
|
|
withPackages = pkgs': let
|
|
|
|
pkgs = builtins.filter (x: x != null) pkgs';
|
2016-10-07 17:31:37 +03:00
|
|
|
extras = map (x: x.emacsBufferSetup pkgs) (builtins.filter (builtins.hasAttr "emacsBufferSetup") pkgs);
|
2016-09-06 00:55:49 +03:00
|
|
|
in writeText "dir-locals.el" ''
|
2016-10-07 17:31:37 +03:00
|
|
|
(require 'inherit-local "${inherit-local}/share/emacs/site-lisp/elpa/inherit-local-${inherit-local.version}/inherit-local.elc")
|
|
|
|
|
|
|
|
; Only set up nixpkgs buffer handling when we have some buffers active
|
|
|
|
(defvar nixpkgs--buffer-count 0)
|
|
|
|
(when (eq nixpkgs--buffer-count 0)
|
2017-03-01 19:00:07 +03:00
|
|
|
(make-variable-buffer-local 'nixpkgs--is-nixpkgs-buffer)
|
2016-10-07 17:31:37 +03:00
|
|
|
; When generating a new temporary buffer (one whose name starts with a space), do inherit-local inheritance and make it a nixpkgs buffer
|
|
|
|
(defun nixpkgs--around-generate (orig name)
|
2017-03-01 19:00:07 +03:00
|
|
|
(if (and nixpkgs--is-nixpkgs-buffer (eq (aref name 0) ?\s))
|
2016-10-07 17:31:37 +03:00
|
|
|
(let ((buf (funcall orig name)))
|
2017-03-01 19:00:07 +03:00
|
|
|
(progn
|
|
|
|
(inherit-local-inherit-child buf)
|
2016-10-07 17:31:37 +03:00
|
|
|
(with-current-buffer buf
|
|
|
|
(setq nixpkgs--buffer-count (1+ nixpkgs--buffer-count))
|
2017-03-01 19:00:07 +03:00
|
|
|
(add-hook 'kill-buffer-hook 'nixpkgs--decrement-buffer-count nil t)))
|
2016-10-07 17:31:37 +03:00
|
|
|
buf)
|
|
|
|
(funcall orig name)))
|
|
|
|
(advice-add 'generate-new-buffer :around #'nixpkgs--around-generate)
|
|
|
|
; When we have no more nixpkgs buffers, tear down the buffer handling
|
|
|
|
(defun nixpkgs--decrement-buffer-count ()
|
|
|
|
(setq nixpkgs--buffer-count (1- nixpkgs--buffer-count))
|
|
|
|
(when (eq nixpkgs--buffer-count 0)
|
|
|
|
(advice-remove 'generate-new-buffer #'nixpkgs--around-generate)
|
|
|
|
(fmakunbound 'nixpkgs--around-generate)
|
|
|
|
(fmakunbound 'nixpkgs--decrement-buffer-count))))
|
|
|
|
(setq nixpkgs--buffer-count (1+ nixpkgs--buffer-count))
|
2017-03-01 19:00:07 +03:00
|
|
|
(add-hook 'kill-buffer-hook 'nixpkgs--decrement-buffer-count nil t)
|
2016-10-07 17:31:37 +03:00
|
|
|
|
|
|
|
; Add packages to PATH and exec-path
|
2016-09-06 00:55:49 +03:00
|
|
|
(make-local-variable 'process-environment)
|
2016-10-07 17:31:37 +03:00
|
|
|
(put 'process-environment 'permanent-local t)
|
|
|
|
(inherit-local 'process-environment)
|
2017-03-25 18:13:25 +03:00
|
|
|
; setenv modifies in place, so copy the environment first
|
|
|
|
(setq process-environment (copy-tree process-environment))
|
2016-09-06 00:55:49 +03:00
|
|
|
(setenv "PATH" (concat "${lib.makeSearchPath "bin" pkgs}:" (getenv "PATH")))
|
2016-10-07 17:31:37 +03:00
|
|
|
(inherit-local-permanent exec-path (append '(${builtins.concatStringsSep " " (map (p: "\"${p}/bin\"") pkgs)}) exec-path))
|
|
|
|
|
2018-02-16 02:30:59 +03:00
|
|
|
(inherit-local-permanent eshell-path-env (concat "${lib.makeSearchPath "bin" pkgs}:" eshell-path-env))
|
|
|
|
|
2017-03-01 19:00:07 +03:00
|
|
|
(setq nixpkgs--is-nixpkgs-buffer t)
|
|
|
|
(inherit-local 'nixpkgs--is-nixpkgs-buffer)
|
|
|
|
|
2016-10-07 17:31:37 +03:00
|
|
|
${lib.concatStringsSep "\n" extras}
|
2016-09-06 00:55:49 +03:00
|
|
|
'';
|
2017-05-01 18:43:53 +03:00
|
|
|
# nix-buffer function for a project with a bunch of haskell packages
|
|
|
|
# in one directory
|
|
|
|
haskellMonoRepo = { project-root # The monorepo root
|
|
|
|
, haskellPackages # The composed haskell packages set that contains all of the packages
|
|
|
|
}: { root }:
|
|
|
|
let # The haskell paths.
|
|
|
|
haskell-paths = lib.filesystem.haskellPathsInDir project-root;
|
|
|
|
# Find the haskell package that the 'root' is in, if any.
|
|
|
|
haskell-path-parent =
|
|
|
|
let filtered = builtins.filter (name:
|
|
|
|
lib.hasPrefix (toString (project-root + "/${name}")) (toString root)
|
|
|
|
) (builtins.attrNames haskell-paths);
|
|
|
|
in
|
|
|
|
if filtered == [] then null else builtins.head filtered;
|
|
|
|
# We're in the directory of a haskell package
|
|
|
|
is-haskell-package = haskell-path-parent != null;
|
|
|
|
haskell-package = haskellPackages.${haskell-path-parent};
|
|
|
|
# GHC environment with all needed deps for the haskell package
|
|
|
|
haskell-package-env =
|
|
|
|
builtins.head haskell-package.env.nativeBuildInputs;
|
|
|
|
in
|
|
|
|
if is-haskell-package
|
|
|
|
then withPackages [ haskell-package-env ]
|
|
|
|
else {};
|
2016-09-05 19:01:26 +03:00
|
|
|
}
|