diff --git a/lib/dag.nix b/lib/dag.nix index 0b2dfc4b..da8ea07d 100644 --- a/lib/dag.nix +++ b/lib/dag.nix @@ -8,13 +8,16 @@ # - the addition of the function `entryBefore` indicating a "wanted # by" relationship. {lib}: let - inherit (lib) all filterAttrs nvim mapAttrs toposort; + inherit (builtins) isAttrs map toJSON isString elem; + inherit (lib.attrsets) attrNames attrValues filterAttrs mapAttrs; + inherit (lib.lists) all toposort; + inherit (lib) nvim; in { empty = {}; isEntry = e: e ? data && e ? after && e ? before; isDag = dag: - builtins.isAttrs dag && all nvim.dag.isEntry (builtins.attrValues dag); + isAttrs dag && all nvim.dag.isEntry (attrValues dag); /* Takes an attribute set containing entries built by entryAnywhere, @@ -76,17 +79,17 @@ in { */ topoSort = dag: let dagBefore = dag: name: - builtins.attrNames - (filterAttrs (_n: v: builtins.elem name v.before) dag); + attrNames + (filterAttrs (_n: v: elem name v.before) dag); normalizedDag = mapAttrs (n: v: { + inherit (v) data; name = n; - data = v.data; after = v.after ++ dagBefore dag n; }) dag; - before = a: b: builtins.elem a.name b.after; - sorted = toposort before (builtins.attrValues normalizedDag); + before = a: b: elem a.name b.after; + sorted = toposort before (attrValues normalizedDag); in if sorted ? result then { @@ -104,4 +107,23 @@ in { entryAfter = nvim.dag.entryBetween []; entryBefore = before: nvim.dag.entryBetween before []; + + resolveDag = { + name, + dag, + mapResult, + }: let + # When the value is a string, default it to dag.entryAnywhere + finalDag = lib.mapAttrs (_: value: + if isString value + then nvim.dag.entryAnywhere value + else value) + dag; + sortedDag = nvim.dag.topoSort finalDag; + result = + if sortedDag ? result + then mapResult sortedDag.result + else abort ("Dependency cycle in ${name}: " + toJSON sortedDag); + in + result; } diff --git a/lib/languages.nix b/lib/languages.nix index 18cfc371..d3f514cd 100644 --- a/lib/languages.nix +++ b/lib/languages.nix @@ -1,4 +1,3 @@ -# From home-manager: https://github.com/nix-community/home-manager/blob/master/modules/lib/booleans.nix {lib}: { # Converts a boolean to a yes/no string. This is used in lots of # configuration formats. diff --git a/modules/core/build/default.nix b/modules/core/build/default.nix index 86239b90..8ef6f93a 100644 --- a/modules/core/build/default.nix +++ b/modules/core/build/default.nix @@ -4,12 +4,13 @@ lib, ... }: let - inherit (builtins) attrValues attrNames map mapAttrs toJSON isString concatStringsSep filter; + inherit (builtins) attrValues attrNames map mapAttrs concatStringsSep filter; inherit (lib) mkOption types mapAttrsFlatten filterAttrs optionalString getAttrs literalExpression; inherit (lib) nvim; inherit (nvim.lua) toLuaObject; inherit (nvim.vim) valToVim; inherit (nvim.bool) mkBool; + inherit (nvim.dag) resolveDag; cfg = config.vim; @@ -179,11 +180,13 @@ in { }; config = let - filterNonNull = mappings: filterAttrs (_name: value: value != null) mappings; globalsScript = mapAttrsFlatten (name: value: "let g:${name}=${valToVim value}") (filterNonNull cfg.globals); + # TODO: everything below this line needs to be moved to lib + filterNonNull = mappings: filterAttrs (_name: value: value != null) mappings; + toLuaBindings = mode: maps: map (value: '' vim.keymap.set(${toLuaObject mode}, ${toLuaObject value.key}, ${toLuaObject value.action}, ${toLuaObject value.config}) @@ -202,24 +205,12 @@ in { omap = toLuaBindings "o" config.vim.maps.operator; icmap = toLuaBindings "ic" config.vim.maps.insertCommand; - resolveDag = { - name, - dag, - mapResult, - }: let - # When the value is a string, default it to dag.entryAnywhere - finalDag = lib.mapAttrs (_: value: - if isString value - then nvim.dag.entryAnywhere value - else value) - dag; - sortedDag = nvim.dag.topoSort finalDag; - result = - if sortedDag ? result - then mapResult sortedDag.result - else abort ("Dependency cycle in ${name}: " + toJSON sortedDag); - in - result; + mkSection = r: '' + -- SECTION: ${r.name} + ${r.data} + ''; + + mapResult = r: (wrapLuaConfig (concatStringsSep "\n" (map mkSection r))); in { vim = { startPlugins = map (x: x.package) (attrValues cfg.extraPlugins); @@ -227,11 +218,6 @@ in { globalsScript = nvim.dag.entryAnywhere (concatStringsSep "\n" globalsScript); luaScript = let - mkSection = r: '' - -- SECTION: ${r.name} - ${r.data} - ''; - mapResult = r: (wrapLuaConfig (concatStringsSep "\n" (map mkSection r))); luaConfig = resolveDag { name = "lua config script"; dag = cfg.luaConfigRC; @@ -241,11 +227,6 @@ in { nvim.dag.entryAfter ["globalsScript"] luaConfig; extraPluginConfigs = let - mkSection = r: '' - -- SECTION: ${r.name} - ${r.data} - ''; - mapResult = r: (wrapLuaConfig (concatStringsSep "\n" (map mkSection r))); extraPluginsDag = mapAttrs (_: { after, setup, @@ -253,6 +234,7 @@ in { }: nvim.dag.entryAfter after setup) cfg.extraPlugins; + pluginConfig = resolveDag { name = "extra plugins config"; dag = extraPluginsDag;