From eca0f17ad20e9604a60efa57c7fa717ee2c8f1b7 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Fri, 7 Oct 2016 10:31:37 -0400 Subject: [PATCH] nix-buffer support improvements. Use inherit-local, add per-package elisp hooks. --- .../science/logic/coq/default.nix | 20 +++++++- pkgs/build-support/cc-wrapper/default.nix | 15 +++++- pkgs/build-support/emacs/buffer.nix | 48 ++++++++++++++----- pkgs/top-level/all-packages.nix | 2 +- 4 files changed, 69 insertions(+), 16 deletions(-) diff --git a/pkgs/applications/science/logic/coq/default.nix b/pkgs/applications/science/logic/coq/default.nix index 38ba14e83cf8..6c4211178077 100644 --- a/pkgs/applications/science/logic/coq/default.nix +++ b/pkgs/applications/science/logic/coq/default.nix @@ -14,8 +14,8 @@ let substituteInPlace plugins/micromega/sos.ml --replace "; csdp" "; ${csdp}/bin/csdp" substituteInPlace plugins/micromega/coq_micromega.ml --replace "System.is_in_system_path \"csdp\"" "true" '' else ""; -in +self = stdenv.mkDerivation { name = "coq-${version}"; @@ -62,6 +62,22 @@ stdenv.mkDerivation { envHooks=(''${envHooks[@]} addCoqPath) ''; + passthru = { + emacsBufferSetup = pkgs: '' + ; Propagate coq paths to children + (inherit-local-permanent coq-prog-name "${self}/bin/coqtop") + (inherit-local-permanent coq-dependency-analyzer "${self}/bin/coqdep") + (inherit-local-permanent coq-compiler "${self}/bin/coqc") + ; If the coq-library path was already set, re-set it based on our current coq + (when (fboundp 'get-coq-library-directory) + (inherit-local-permanent coq-library-directory (get-coq-library-directory)) + (coq-prog-args)) + ; Pass proof-general's coq flags to flycheck command (pretty ugly, should probably be part of PG) + (inherit-local-permanent flycheck-command-wrapper-function (lambda (cmd) + (append (funcall (default-value 'flycheck-command-wrapper-function) cmd) (coq-coqtop-prog-args coq-load-path)))) + ''; + }; + meta = with stdenv.lib; { description = "Formal proof management system"; longDescription = '' @@ -76,4 +92,4 @@ stdenv.mkDerivation { maintainers = with maintainers; [ roconnor thoughtpolice vbgl ]; platforms = platforms.unix; }; -} +}; in self diff --git a/pkgs/build-support/cc-wrapper/default.nix b/pkgs/build-support/cc-wrapper/default.nix index 8a746ea016ef..8a9bd3ecb4d5 100644 --- a/pkgs/build-support/cc-wrapper/default.nix +++ b/pkgs/build-support/cc-wrapper/default.nix @@ -46,7 +46,20 @@ stdenv.mkDerivation { inherit cc shell libc_bin libc_dev libc_lib binutils_bin coreutils_bin; gnugrep_bin = if nativeTools then "" else gnugrep; - passthru = { inherit libc nativeTools nativeLibc nativePrefix isGNU isClang; }; + passthru = { + inherit libc nativeTools nativeLibc nativePrefix isGNU isClang; + + emacsBufferSetup = pkgs: '' + ; We should handle propagation here too + (mapc (lambda (arg) + (when (file-directory-p (concat arg "/include")) + (setenv "NIX_CFLAGS_COMPILE" (concat (getenv "NIX_CFLAGS_COMPILE") " -isystem " arg "/include"))) + (when (file-directory-p (concat arg "/lib")) + (setenv "NIX_LDFLAGS" (concat (getenv "NIX_LDFLAGS") " -L" arg "/lib"))) + (when (file-directory-p (concat arg "/lib64")) + (setenv "NIX_LDFLAGS" (concat (getenv "NIX_LDFLAGS") " -L" arg "/lib64")))) '(${concatStringsSep " " (map (pkg: "\"${pkg}\"") pkgs)})) + ''; + }; buildCommand = '' diff --git a/pkgs/build-support/emacs/buffer.nix b/pkgs/build-support/emacs/buffer.nix index 5632eae944c0..e366fd1f739f 100644 --- a/pkgs/build-support/emacs/buffer.nix +++ b/pkgs/build-support/emacs/buffer.nix @@ -1,23 +1,47 @@ # Functions to build elisp files to locally configure emcas buffers. # See https://github.com/shlevy/nix-buffer -{ lib, writeText }: +{ lib, writeText, inherit-local }: { withPackages = pkgs: let - coqs = builtins.filter (x: (builtins.parseDrvName x.name).name == "coq") pkgs; - coq = builtins.head coqs; - pg-setup = if builtins.length coqs == 0 then "" else '' - (setq-local coq-prog-name "${coq}/bin/coqtop") - (setq-local coq-dependency-analyzer "${coq}/bin/coqdep") - (setq-local coq-compiler "${coq}/bin/coqc") - (setq-local coq-library-directory (get-coq-library-directory)) - (coq-prog-args) - ''; + extras = map (x: x.emacsBufferSetup pkgs) (builtins.filter (builtins.hasAttr "emacsBufferSetup") pkgs); in writeText "dir-locals.el" '' + (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) + ; 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) + (if (eq (aref name 0) ?\s) + (let ((buf (funcall orig name))) + (when (inherit-local-inherit-child buf) + (with-current-buffer buf + (make-local-variable 'kill-buffer-hook) + (setq nixpkgs--buffer-count (1+ nixpkgs--buffer-count)) + (add-hook 'kill-buffer-hook 'nixpkgs--decrement-buffer-count))) + 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)) + (make-local-variable 'kill-buffer-hook) + (add-hook 'kill-buffer-hook 'nixpkgs--decrement-buffer-count) + + ; Add packages to PATH and exec-path (make-local-variable 'process-environment) + (put 'process-environment 'permanent-local t) + (inherit-local 'process-environment) (setenv "PATH" (concat "${lib.makeSearchPath "bin" pkgs}:" (getenv "PATH"))) - (setq-local exec-path (append '(${builtins.concatStringsSep " " (map (p: "\"${p}/bin\"") pkgs)}) exec-path)) - ${pg-setup} + (inherit-local-permanent exec-path (append '(${builtins.concatStringsSep " " (map (p: "\"${p}/bin\"") pkgs)}) exec-path)) + + ${lib.concatStringsSep "\n" extras} ''; } diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 71fb0899a288..c96a10136af3 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -287,7 +287,7 @@ in inherit kernel rootModules allowMissing; }; - nixBufferBuilders = import ../build-support/emacs/buffer.nix { inherit (pkgs) lib writeText; }; + nixBufferBuilders = import ../build-support/emacs/buffer.nix { inherit (pkgs) lib writeText; inherit (emacsPackagesNg) inherit-local; }; pathsFromGraph = ../build-support/kernel/paths-from-graph.pl;