mirror of
https://github.com/nix-community/emacs-overlay.git
synced 2025-12-06 02:40:25 -08:00
Make parsePackagesFromUsePackage consider :ensure and :disabled
Use `fromElisp` (https://github.com/talyz/fromElisp), imported using `niv` (https://github.com/nmattia/niv), to properly parse the emacs configuration. This lets us take `:ensure` and `:disabled` into account when listing packages to install: - only declarations with an `:ensure` value of true or an alternate package name will be included; if an alternate package name is supplied, it will be used - declarations with a `:disabled` value of true will be excluded
This commit is contained in:
parent
419d8e4636
commit
8bdbed0579
3 changed files with 238 additions and 22 deletions
14
nix/sources.json
Normal file
14
nix/sources.json
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"fromElisp": {
|
||||
"branch": "master",
|
||||
"description": "An Emacs Lisp reader in Nix.",
|
||||
"homepage": "",
|
||||
"owner": "talyz",
|
||||
"repo": "fromElisp",
|
||||
"rev": "de85d2e2b7ff6b3260e14b799404dc531113f534",
|
||||
"sha256": "1s520nv04nl97qqz61s2a8xj72hz0vi588sk9r11zrmw6a14phfj",
|
||||
"type": "tarball",
|
||||
"url": "https://github.com/talyz/fromElisp/archive/de85d2e2b7ff6b3260e14b799404dc531113f534.tar.gz",
|
||||
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
|
||||
}
|
||||
}
|
||||
134
nix/sources.nix
Normal file
134
nix/sources.nix
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
# This file has been generated by Niv.
|
||||
|
||||
let
|
||||
|
||||
#
|
||||
# The fetchers. fetch_<type> fetches specs of type <type>.
|
||||
#
|
||||
|
||||
fetch_file = pkgs: spec:
|
||||
if spec.builtin or true then
|
||||
builtins_fetchurl { inherit (spec) url sha256; }
|
||||
else
|
||||
pkgs.fetchurl { inherit (spec) url sha256; };
|
||||
|
||||
fetch_tarball = pkgs: spec:
|
||||
if spec.builtin or true then
|
||||
builtins_fetchTarball { inherit (spec) url sha256; }
|
||||
else
|
||||
pkgs.fetchzip { inherit (spec) url sha256; };
|
||||
|
||||
fetch_git = spec:
|
||||
builtins.fetchGit { url = spec.repo; inherit (spec) rev ref; };
|
||||
|
||||
fetch_builtin-tarball = spec:
|
||||
builtins.trace
|
||||
''
|
||||
WARNING:
|
||||
The niv type "builtin-tarball" will soon be deprecated. You should
|
||||
instead use `builtin = true`.
|
||||
|
||||
$ niv modify <package> -a type=tarball -a builtin=true
|
||||
''
|
||||
builtins_fetchTarball { inherit (spec) url sha256; };
|
||||
|
||||
fetch_builtin-url = spec:
|
||||
builtins.trace
|
||||
''
|
||||
WARNING:
|
||||
The niv type "builtin-url" will soon be deprecated. You should
|
||||
instead use `builtin = true`.
|
||||
|
||||
$ niv modify <package> -a type=file -a builtin=true
|
||||
''
|
||||
(builtins_fetchurl { inherit (spec) url sha256; });
|
||||
|
||||
#
|
||||
# Various helpers
|
||||
#
|
||||
|
||||
# The set of packages used when specs are fetched using non-builtins.
|
||||
mkPkgs = sources:
|
||||
let
|
||||
sourcesNixpkgs =
|
||||
import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) {};
|
||||
hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath;
|
||||
hasThisAsNixpkgsPath = <nixpkgs> == ./.;
|
||||
in
|
||||
if builtins.hasAttr "nixpkgs" sources
|
||||
then sourcesNixpkgs
|
||||
else if hasNixpkgsPath && ! hasThisAsNixpkgsPath then
|
||||
import <nixpkgs> {}
|
||||
else
|
||||
abort
|
||||
''
|
||||
Please specify either <nixpkgs> (through -I or NIX_PATH=nixpkgs=...) or
|
||||
add a package called "nixpkgs" to your sources.json.
|
||||
'';
|
||||
|
||||
# The actual fetching function.
|
||||
fetch = pkgs: name: spec:
|
||||
|
||||
if ! builtins.hasAttr "type" spec then
|
||||
abort "ERROR: niv spec ${name} does not have a 'type' attribute"
|
||||
else if spec.type == "file" then fetch_file pkgs spec
|
||||
else if spec.type == "tarball" then fetch_tarball pkgs spec
|
||||
else if spec.type == "git" then fetch_git spec
|
||||
else if spec.type == "builtin-tarball" then fetch_builtin-tarball spec
|
||||
else if spec.type == "builtin-url" then fetch_builtin-url spec
|
||||
else
|
||||
abort "ERROR: niv spec ${name} has unknown type ${builtins.toJSON spec.type}";
|
||||
|
||||
# Ports of functions for older nix versions
|
||||
|
||||
# a Nix version of mapAttrs if the built-in doesn't exist
|
||||
mapAttrs = builtins.mapAttrs or (
|
||||
f: set: with builtins;
|
||||
listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set))
|
||||
);
|
||||
|
||||
# fetchTarball version that is compatible between all the versions of Nix
|
||||
builtins_fetchTarball = { url, sha256 }@attrs:
|
||||
let
|
||||
inherit (builtins) lessThan nixVersion fetchTarball;
|
||||
in
|
||||
if lessThan nixVersion "1.12" then
|
||||
fetchTarball { inherit url; }
|
||||
else
|
||||
fetchTarball attrs;
|
||||
|
||||
# fetchurl version that is compatible between all the versions of Nix
|
||||
builtins_fetchurl = { url, sha256 }@attrs:
|
||||
let
|
||||
inherit (builtins) lessThan nixVersion fetchurl;
|
||||
in
|
||||
if lessThan nixVersion "1.12" then
|
||||
fetchurl { inherit url; }
|
||||
else
|
||||
fetchurl attrs;
|
||||
|
||||
# Create the final "sources" from the config
|
||||
mkSources = config:
|
||||
mapAttrs (
|
||||
name: spec:
|
||||
if builtins.hasAttr "outPath" spec
|
||||
then abort
|
||||
"The values in sources.json should not have an 'outPath' attribute"
|
||||
else
|
||||
spec // { outPath = fetch config.pkgs name spec; }
|
||||
) config.sources;
|
||||
|
||||
# The "config" used by the fetchers
|
||||
mkConfig =
|
||||
{ sourcesFile ? ./sources.json
|
||||
, sources ? builtins.fromJSON (builtins.readFile sourcesFile)
|
||||
, pkgs ? mkPkgs sources
|
||||
}: rec {
|
||||
# The sources, i.e. the attribute set of spec name to spec
|
||||
inherit sources;
|
||||
|
||||
# The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers
|
||||
inherit pkgs;
|
||||
};
|
||||
in
|
||||
mkSources (mkConfig {}) // { __functor = _: settings: mkSources (mkConfig settings); }
|
||||
112
parse.nix
112
parse.nix
|
|
@ -1,5 +1,9 @@
|
|||
{ lib }:
|
||||
|
||||
let
|
||||
nixDeps = import ./nix/sources.nix;
|
||||
inherit (import nixDeps.fromElisp {}) fromElisp;
|
||||
|
||||
isStrEmpty = s: (builtins.replaceStrings [ " " ] [ "" ] s) == "";
|
||||
|
||||
splitString = _sep: _s: builtins.filter
|
||||
|
|
@ -41,31 +45,95 @@ let
|
|||
in
|
||||
parseReqList requires;
|
||||
|
||||
stripComments = dotEmacs:
|
||||
# Get a list of packages declared wanted with `use-package` in the
|
||||
# input string `config`. The goal is to only list packages that
|
||||
# would be installed by `use-package` on evaluation; thus we look at
|
||||
# the `:ensure` and `:disabled` keyword values to attempt to figure
|
||||
# out which and whether the package should be installed.
|
||||
#
|
||||
# Example input:
|
||||
#
|
||||
# ''
|
||||
# (use-package org
|
||||
# :commands org-mode
|
||||
# :bind (("C-c a" . org-agenda)
|
||||
# :map org-mode-map
|
||||
# ([C-right] . org-demote-subtree)
|
||||
# ([C-left] . org-promote-subtree)))
|
||||
#
|
||||
# (use-package direnv
|
||||
# :ensure t
|
||||
# :config (direnv-mode))
|
||||
#
|
||||
# (use-package paredit-mode
|
||||
# :ensure paredit
|
||||
# :hook (emacs-lisp-mode lisp-mode lisp-interaction-mode))
|
||||
# ''
|
||||
# => [ "direnv" "paredit" ]
|
||||
parsePackagesFromUsePackage = config:
|
||||
let
|
||||
lines = splitString "\n" dotEmacs;
|
||||
stripped = builtins.map
|
||||
(l:
|
||||
builtins.elemAt (splitString ";;" l) 0)
|
||||
lines;
|
||||
in
|
||||
builtins.concatStringsSep " " stripped;
|
||||
find = item: list:
|
||||
if list == [] then [] else
|
||||
if builtins.head list == item then
|
||||
list
|
||||
else
|
||||
find item (builtins.tail list);
|
||||
|
||||
parsePackagesFromUsePackage = dotEmacs:
|
||||
let
|
||||
strippedComments = stripComments dotEmacs;
|
||||
tokens = builtins.filter (t: !(isStrEmpty t)) (builtins.map
|
||||
(t: if builtins.typeOf t == "list" then builtins.elemAt t 0 else t)
|
||||
(builtins.split "([\(\)])" strippedComments)
|
||||
);
|
||||
matches = builtins.map
|
||||
(t:
|
||||
builtins.match "^use-package[[:space:]]+([A-Za-z0-9_-]+).*" t)
|
||||
tokens;
|
||||
getKeywordValue = keyword: list:
|
||||
let
|
||||
keywordList = find keyword list;
|
||||
in
|
||||
if keywordList != [] then
|
||||
let
|
||||
keywordValue = builtins.tail keywordList;
|
||||
in
|
||||
if keywordValue != [] then
|
||||
builtins.head keywordValue
|
||||
else
|
||||
true
|
||||
else
|
||||
null;
|
||||
|
||||
isDisabled = item:
|
||||
let
|
||||
disabledValue = getKeywordValue ":disabled" item;
|
||||
in
|
||||
if disabledValue == [] then
|
||||
false
|
||||
else if builtins.isBool disabledValue then
|
||||
disabledValue
|
||||
else if builtins.isString disabledValue then
|
||||
true
|
||||
else
|
||||
false;
|
||||
|
||||
getName = item:
|
||||
let
|
||||
ensureValue = getKeywordValue ":ensure" item;
|
||||
in
|
||||
if ensureValue == [] then
|
||||
[]
|
||||
else if builtins.isString ensureValue && !(lib.hasPrefix ":" ensureValue) then
|
||||
ensureValue
|
||||
else
|
||||
builtins.head (builtins.tail item);
|
||||
|
||||
recurse = item:
|
||||
if builtins.isList item && item != [] then
|
||||
if (builtins.head item) == "use-package" then
|
||||
if !(isDisabled item) then
|
||||
if builtins.elem ":ensure" item then
|
||||
[ (getName item) ] ++ map recurse item
|
||||
else
|
||||
map recurse item
|
||||
else
|
||||
[]
|
||||
else
|
||||
map recurse item
|
||||
else
|
||||
[];
|
||||
in
|
||||
builtins.map
|
||||
(m: builtins.elemAt m 0)
|
||||
(builtins.filter (m: m != null) matches);
|
||||
lib.flatten (map recurse (fromElisp config));
|
||||
|
||||
in
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue