From 919cab94184dc38795a00ec8371bc4580411a38a Mon Sep 17 00:00:00 2001 From: Pacman99 Date: Mon, 29 Mar 2021 10:36:11 -0700 Subject: [PATCH 1/9] mkHosts: get devos arg for core and cachix --- lib/devos/mkHosts.nix | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/devos/mkHosts.nix b/lib/devos/mkHosts.nix index 1d1bfce0e..e7664012c 100644 --- a/lib/devos/mkHosts.nix +++ b/lib/devos/mkHosts.nix @@ -1,6 +1,6 @@ { lib, dev, nixos, inputs, self, ... }: -{ dir, extern, suites, overrides, multiPkgs, ... }: +{ dir, devos, extern, suites, overrides, multiPkgs, ... }: let defaultSystem = "x86_64-linux"; @@ -12,7 +12,7 @@ let ]; modules = { - core = "${self}/profiles/core"; + core = "${devos}/profiles/core"; modOverrides = { config, overrideModulesPath, ... }: let inherit (overrides) modules disabledModules; @@ -61,7 +61,7 @@ let # Everything in `./modules/list.nix`. flakeModules = { imports = builtins.attrValues self.nixosModules ++ extern.modules; }; - cachix = ../../cachix.nix; + cachix = "${devos}/cachix.nix"; }; specialArgs = extern.specialArgs // { suites = suites.system; }; From b01f5d10936a265ccda86584670b50f49560bc99 Mon Sep 17 00:00:00 2001 From: Pacman99 Date: Fri, 26 Mar 2021 11:57:24 -0700 Subject: [PATCH 2/9] lib: init evalFlakeArgs for mkFlake args module describing devos flake arguments --- lib/default.nix | 2 + lib/mkFlake/evalArgs.nix | 182 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 184 insertions(+) create mode 100644 lib/mkFlake/evalArgs.nix diff --git a/lib/default.nix b/lib/default.nix index 8ed28a43d..45c7ff858 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -17,6 +17,8 @@ lib.makeExtensible (final: lists = callLibs ./lists.nix; strings = callLibs ./strings.nix; + evalFlakeArgs = callLibs ./mkFlake/evalArgs.nix; + inherit (attrs) mapFilterAttrs genAttrs' safeReadDir pathsToImportedAttrs concatAttrs filterPackages; inherit (lists) pathsIn; diff --git a/lib/mkFlake/evalArgs.nix b/lib/mkFlake/evalArgs.nix new file mode 100644 index 000000000..69cc665cc --- /dev/null +++ b/lib/mkFlake/evalArgs.nix @@ -0,0 +1,182 @@ +{ self, dev, nixos, inputs, ... }: + +{ args }: +let + argOpts = with nixos.lib; { config, options, ... }: + let + inherit (dev) os; + + inherit (config) self; + + inputAttrs = with types; functionTo attrs; + moduleType = with types; anything // { + inherit (submodule {}) check; + description = "valid module"; + }; + in + { + options = with types; { + self = mkOption { + type = addCheck attrs nixos.lib.isStorePath; + description = "The flake to create the devos outputs for"; + }; + hosts = mkOption { + type = path; + default = "${self}/hosts"; + defaultText = "\${self}/hosts"; + apply = toString; + description = '' + Path to directory containing host configurations that will be exported + to the 'nixosConfigurations' output. + ''; + }; + packages = mkOption { + # functionTo changes arg names which breaks flake check + type = types.anything // { + check = builtins.isFunction; + description = "Nixpkgs overlay"; + }; + default = (final: prev: {}); + defaultText = "(final: prev: {})"; + description = '' + Overlay for custom packages that will be included in treewide 'pkgs'. + This should follow the standard nixpkgs overlay format - two argument function + that returns an attrset. + These packages will be exported to the 'packages' and 'legacyPackages' outputs. + ''; + }; + modules = mkOption { + type = listOf moduleType; + default = []; + apply = dev.pathsToImportedAttrs; + description = '' + list of modules to include in confgurations and export in 'nixosModules' output + ''; + }; + userModules = mkOption { + type = listOf moduleType; + default = []; + apply = dev.pathsToImportedAttrs; + description = '' + list of modules to include in home-manager configurations and export in + 'homeModules' output + ''; + }; + profiles = mkOption { + type = path; + default = "${self}/profiles"; + defaultText = "\${self}/profiles"; + apply = x: os.mkProfileAttrs (toString x); + description = "path to profiles folder that can be collected into suites"; + }; + userProfiles = mkOption { + type = path; + default = "${self}/users/profiles"; + defaultText = "\${self}/users/profiles"; + apply = x: os.mkProfileAttrs (toString x); + description = "path to user profiles folder that can be collected into userSuites"; + }; + suites = + let + defaults = { user = {}; system = {}; }; + in + mkOption { + type = inputAttrs; + default = { ... }: defaults; + defaultText = "{ user = {}; system = {}; }"; + apply = suites: defaults // os.mkSuites { + inherit suites; + inherit (config) profiles users userProfiles; + }; + description = '' + Function with inputs 'users' and 'profiles' that returns attribute set + with user and system suites. The former for Home Manager and the latter + for nixos configurations. + These can be accessed through the 'suites' specialArg in each config system. + ''; + }; + users = mkOption { + type = path; + default = "${self}/users"; + defaultText = "\${self}/users"; + apply = x: os.mkProfileAttrs (toString x); + description = '' + path to folder containing profiles that define system users + ''; + }; + extern = + let + defaults = { + modules = []; overlays = []; specialArgs = {}; + userModules = []; userSpecialArgs = []; + }; + in + mkOption { + type = inputAttrs; + default = { ... }: defaults; + defaultText = '' + { modules = []; overlays = []; specialArgs = []; userModules = []; userSpecialArgs = []; } + ''; + # So unneeded extern attributes can safely be deleted + apply = x: defaults // (x { inputs = inputs // self.inputs; }); + description = '' + Function with argument 'inputs' that contains all devos and ''${self}'s inputs. + The function should return an attribute set with modules, overlays, and + specialArgs to be included across nixos and home manager configurations. + Only attributes that are used should be returned. + ''; + }; + overlays = mkOption { + type = path; + default = "${self}/overlays"; + defaultText = "\${self}/overlays"; + apply = x: dev.pathsToImportedAttrs (dev.pathsIn (toString x)); + description = '' + path to folder containing overlays which will be applied to pkgs and exported in + the 'overlays' output + ''; + }; + overrides = mkOption rec { + type = attrs; + default = { modules = []; disabledModules = []; packages = _: _: _: {}; }; + defaultText = "{ modules = []; disabledModules = []; packages = {}; }"; + apply = x: default // x; + description = "attrset of packages and modules that will be pulled from nixpkgs master"; + }; + genDoc = mkOption { + type = functionTo attrs; + internal = true; + description = "function that returns a generated options doc derivation given nixpkgs"; + }; + }; + config.genDoc = + let + singleDoc = name: value: '' + ## ${name} + ${value.description} + + ${optionalString (value ? type) '' + *_Type_*: + ${value.type} + ''} + ${optionalString (value ? default) '' + *_Default_* + ``` + ${builtins.toJSON value.default} + ``` + ''} + ${optionalString (value ? example) '' + *_Example_* + ``` + ${value.example} + ``` + ''} + ''; + in + pkgs: with pkgs; writeText "devosOptions.md" + (concatStringsSep "" (mapAttrsToList singleDoc (nixosOptionsDoc { inherit options; }).optionsNix)); + }; +in + nixos.lib.evalModules { + modules = [ argOpts args ]; + } From 09ca1b60b194447accda50798b646a1418e2f63f Mon Sep 17 00:00:00 2001 From: Pacman99 Date: Tue, 30 Mar 2021 11:10:58 -0700 Subject: [PATCH 3/9] lib: init mkFlake to create a devos flake general lib function - not devos specific --- lib/default.nix | 1 + lib/mkFlake/default.nix | 64 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 lib/mkFlake/default.nix diff --git a/lib/default.nix b/lib/default.nix index 45c7ff858..b9a7a5071 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -17,6 +17,7 @@ lib.makeExtensible (final: lists = callLibs ./lists.nix; strings = callLibs ./strings.nix; + mkFlake = callLibs ./mkFlake; evalFlakeArgs = callLibs ./mkFlake/evalArgs.nix; inherit (attrs) mapFilterAttrs genAttrs' safeReadDir diff --git a/lib/mkFlake/default.nix b/lib/mkFlake/default.nix new file mode 100644 index 000000000..ece286ad1 --- /dev/null +++ b/lib/mkFlake/default.nix @@ -0,0 +1,64 @@ +{ self, nixos, inputs, ... }: +let + devos = self; +in + +{ self, ... } @ args: +let + inherit (self) lib; + inherit (lib) os; + + inherit (inputs) utils deploy; + + cfg = (lib.evalFlakeArgs { inherit args; }).config; + + multiPkgs = os.mkPkgs { inherit (cfg) extern overrides; }; + + outputs = { + nixosConfigurations = os.mkHosts { + inherit devos multiPkgs; + inherit (cfg) extern suites overrides; + dir = cfg.hosts; + }; + + homeConfigurations = os.mkHomeConfigurations; + + nixosModules = cfg.modules; + + homeModules = cfg.userModules; + + overlay = cfg.packages; + inherit (cfg) overlays; + + lib = import "${devos}/lib" { inherit self nixos inputs; }; + + deploy.nodes = os.mkNodes deploy self.nixosConfigurations; + }; + + systemOutputs = utils.lib.eachDefaultSystem (system: + let + pkgs = multiPkgs.${system}; + # all packages that are defined in ./pkgs + legacyPackages = os.mkPackages { inherit pkgs; }; + in + { + checks = + let + tests = nixos.lib.optionalAttrs (system == "x86_64-linux") + (import "${devos}/tests" { inherit pkgs; self = devos; }); + deployHosts = nixos.lib.filterAttrs + (n: _: self.nixosConfigurations.${n}.config.nixpkgs.system == system) self.deploy.nodes; + deployChecks = deploy.lib.${system}.deployChecks { nodes = deployHosts; }; + in + nixos.lib.recursiveUpdate tests deployChecks; + + inherit legacyPackages; + packages = lib.filterPackages system legacyPackages; + + devShell = import "${devos}/shell" { + inherit self system; + }; + }); +in + nixos.lib.recursiveUpdate outputs systemOutputs + From 3e7b1831fbc4c09e122160412d6313c3dd555551 Mon Sep 17 00:00:00 2001 From: Pacman99 Date: Fri, 26 Mar 2021 11:59:10 -0700 Subject: [PATCH 4/9] flake: use mkFlake this is where we create devos's template structure --- flake.nix | 93 ++++++++++++++----------------------------------------- 1 file changed, 24 insertions(+), 69 deletions(-) diff --git a/flake.nix b/flake.nix index 2af75209e..7b70b4a25 100644 --- a/flake.nix +++ b/flake.nix @@ -28,79 +28,34 @@ pkgs.inputs.nixpkgs.follows = "nixos"; }; - outputs = inputs@{ deploy, nixos, nur, self, utils, ... }: - let - inherit (self) lib; - inherit (lib) os; - - extern = import ./extern { inherit inputs; }; - overrides = import ./overrides; - - multiPkgs = os.mkPkgs { - inherit extern overrides; - }; - - suites = os.mkSuites { - suites = import ./suites; - users = os.mkProfileAttrs "${self}/users"; - profiles = os.mkProfileAttrs "${self}/profiles"; - userProfiles = os.mkProfileAttrs "${self}/users/profiles"; - }; - - outputs = { - nixosConfigurations = os.mkHosts { - dir = "${self}/hosts"; + outputs = inputs@{ deploy, nixos, nur, self, utils, ... }: + let + lib = import ./lib { inherit self nixos inputs; }; + + out = lib.mkFlake { + inherit self; + hosts = ./hosts; + packages = import ./pkgs; + suites = import ./suites; + extern = import ./extern; overrides = import ./overrides; - inherit multiPkgs suites extern; + overlays = ./overlays; + profiles = ./profiles; + userProfiles = ./users/profiles; + modules = import ./modules/module-list.nix; + userModules = import ./users/modules/module-list.nix; }; - homeConfigurations = os.mkHomeConfigurations; - - nixosModules = - let moduleList = import ./modules/module-list.nix; - in lib.pathsToImportedAttrs moduleList; - - homeModules = - let moduleList = import ./users/modules/module-list.nix; - in lib.pathsToImportedAttrs moduleList; - - overlay = import ./pkgs; - overlays = lib.pathsToImportedAttrs (lib.pathsIn ./overlays); - - lib = import ./lib { inherit nixos self inputs; }; - + in nixos.lib.recursiveUpdate out { + defaultTemplate = self.templates.flk; templates.flk.path = ./.; templates.flk.description = "flk template"; - defaultTemplate = self.templates.flk; - - deploy.nodes = os.mkNodes deploy self.nixosConfigurations; + templates.mkflake.path = + let + excludes = [ "lib" "tests" "cachix" "nix" "theme" ".github" "bors.toml" "cachix.nix" ]; + filter = path: type: ! builtins.elem (baseNameOf path) excludes; + in + builtins.filterSource filter ./.; + templates.mkflake.description = "template with necessary folders for mkFlake usage"; }; - - systemOutputs = utils.lib.eachDefaultSystem (system: - let - pkgs = multiPkgs.${system}; - # all packages that are defined in ./pkgs - legacyPackages = os.mkPackages { inherit pkgs; }; - in - { - checks = - let - tests = nixos.lib.optionalAttrs (system == "x86_64-linux") - (import ./tests { inherit self pkgs; }); - deployHosts = nixos.lib.filterAttrs - (n: _: self.nixosConfigurations.${n}.config.nixpkgs.system == system) self.deploy.nodes; - deployChecks = deploy.lib.${system}.deployChecks { nodes = deployHosts; }; - in - nixos.lib.recursiveUpdate tests deployChecks; - - inherit legacyPackages; - packages = lib.filterPackages system legacyPackages; - - devShell = import ./shell { - inherit self system extern overrides; - }; - } - ); - in - nixos.lib.recursiveUpdate outputs systemOutputs; } From 2161153b34b4d51b6cefc0c18a4aa3a385b3bebe Mon Sep 17 00:00:00 2001 From: Pacman99 Date: Fri, 26 Mar 2021 12:25:41 -0700 Subject: [PATCH 5/9] pkgs: add devosOptionsDoc - devos specific --- flake.lock | 2 +- pkgs/default.nix | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/flake.lock b/flake.lock index 399626897..dad9766c6 100644 --- a/flake.lock +++ b/flake.lock @@ -216,7 +216,7 @@ ] }, "locked": { - "narHash": "sha256-XG4TOZObj2Wd8KiqnHgtlWjjMbJOIJB7+DxUFzMCXw8=", + "narHash": "sha256-8Xx9gpptVG5H114Tpv+Xfn1gUtXcDUaUnjH3x3KFiso=", "path": "./pkgs", "type": "path" }, diff --git a/pkgs/default.nix b/pkgs/default.nix index ce1687071..c53578234 100644 --- a/pkgs/default.nix +++ b/pkgs/default.nix @@ -1 +1,3 @@ -final: prev: { } +final: prev: { + devosOptionsDoc = (prev.lib.dev.evalFlakeArgs { args = {}; }).config.genDoc prev; +} From c30cf5bea4b5ddb578e4d476b036b373a6754883 Mon Sep 17 00:00:00 2001 From: Pacman99 Date: Tue, 23 Mar 2021 13:16:08 -0700 Subject: [PATCH 6/9] doc/start: add section for using mkflake --- doc/start/index.md | 1 + doc/start/mkflake.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 doc/start/mkflake.md diff --git a/doc/start/index.md b/doc/start/index.md index 48d2b16f7..9fa0061cf 100644 --- a/doc/start/index.md +++ b/doc/start/index.md @@ -33,6 +33,7 @@ In addition, the [binary cache](../../cachix) is added for faster deployment. > upstream changes. ## Next Steps: +- [Use Devos as a library!](./mkflake.md) - [Make installable ISO](./iso.md) - [Bootstrap Host](./bootstrapping.md) - [Already on NixOS](./from-nixos.md) diff --git a/doc/start/mkflake.md b/doc/start/mkflake.md new file mode 100644 index 000000000..6b2236271 --- /dev/null +++ b/doc/start/mkflake.md @@ -0,0 +1,42 @@ +# Use Devos as a library! +You can also add devos as a flake input and use its library function, `mkFlake` to +create your flake. This gives you the advantage of using nix flakes to sync with +upstream changes in devos. + +You can either use the default template or use the 'mkflake' template which only +includes the necessary folders for `mkFlake` usage. It can be pulled with: +```sh +nix flake init -t github:divnix/devos#mkflake +``` + +Once you have a template, you need to add devos as a flake input, which would look +like this: +```nix +inputs = { + ... + devos.url = "github:divnix/devos"; +}; +``` +> ##### Note: +> - All devos inputs must still be included in your flake, due to a nix +> [issue](https://github.com/NixOS/nix/pull/4641) with the `follows` attribute. +> - You can append `/community` to access community modules [extern](../../extern). + +You can then call `mkFlake` to create your outputs. Here is a simple example: +```nix +outputs = { self, devos, ... }: devos.lib.mkFlake { + inherit self; + hosts = ./hosts; +}; +``` +`mkFlake` has various arguments to include more devos concepts like suites and profiles. +These options are documented in [mkFlakeOptions](../mkFlakeOptions.md). + +The devos template itself uses mkFlake to export its own outputs, so you can take +a look at this repository's [flake.nix](../../flake.nix) for a more realistic use +of `mkFlake`. + +You can now sync with upstream devos changes just like any other flake input. But +you will no longer be able to edit devos's internals, since you are directly following +upstream devos changes. + From a868ed104189149c64151c2e1f238528bf75a6b3 Mon Sep 17 00:00:00 2001 From: Pacman99 Date: Fri, 26 Mar 2021 22:29:07 -0700 Subject: [PATCH 7/9] doc: init mkFlakeOptions.md generated mkFlake options documentation --- doc/mkFlakeOptions.md | 160 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 doc/mkFlakeOptions.md diff --git a/doc/mkFlakeOptions.md b/doc/mkFlakeOptions.md new file mode 100644 index 000000000..529d0027e --- /dev/null +++ b/doc/mkFlakeOptions.md @@ -0,0 +1,160 @@ +## extern +Function with argument 'inputs' that contains all devos and ${self}'s inputs. +The function should return an attribute set with modules, overlays, and +specialArgs to be included across nixos and home manager configurations. +Only attributes that are used should be returned. + + +*_Type_*: +function that evaluates to a(n) attrs + +*_Default_* +``` +"{ modules = []; overlays = []; specialArgs = []; userModules = []; userSpecialArgs = []; }\n" +``` + + +## hosts +Path to directory containing host configurations that will be exported +to the 'nixosConfigurations' output. + + +*_Type_*: +path + +*_Default_* +``` +"${self}/hosts" +``` + + +## modules +list of modules to include in confgurations and export in 'nixosModules' output + + +*_Type_*: +list of valid modules + +*_Default_* +``` +[] +``` + + +## overlays +path to folder containing overlays which will be applied to pkgs and exported in +the 'overlays' output + + +*_Type_*: +path + +*_Default_* +``` +"${self}/overlays" +``` + + +## overrides +attrset of packages and modules that will be pulled from nixpkgs master + +*_Type_*: +attribute set + +*_Default_* +``` +"{ modules = []; disabledModules = []; packages = {}; }" +``` + + +## packages +Overlay for custom packages that will be included in treewide 'pkgs'. +This should follow the standard nixpkgs overlay format - two argument function +that returns an attrset. +These packages will be exported to the 'packages' and 'legacyPackages' outputs. + + +*_Type_*: +Nixpkgs overlay + +*_Default_* +``` +"(final: prev: {})" +``` + + +## profiles +path to profiles folder that can be collected into suites + +*_Type_*: +path + +*_Default_* +``` +"${self}/profiles" +``` + + +## self +The flake to create the devos outputs for + +*_Type_*: +attribute set + + + +## suites +Function with inputs 'users' and 'profiles' that returns attribute set +with user and system suites. The former for Home Manager and the latter +for nixos configurations. +These can be accessed through the 'suites' specialArg in each config system. + + +*_Type_*: +function that evaluates to a(n) attrs + +*_Default_* +``` +"{ user = {}; system = {}; }" +``` + + +## userModules +list of modules to include in home-manager configurations and export in +'homeModules' output + + +*_Type_*: +list of valid modules + +*_Default_* +``` +[] +``` + + +## userProfiles +path to user profiles folder that can be collected into userSuites + +*_Type_*: +path + +*_Default_* +``` +"${self}/users/profiles" +``` + + +## users +path to folder containing profiles that define system users + + +*_Type_*: +path + +*_Default_* +``` +"${self}/users" +``` + + From 683c606cd1f488a2b67d243f2392c7cc6fc9f71b Mon Sep 17 00:00:00 2001 From: Pacman99 Date: Thu, 1 Apr 2021 11:30:45 -0700 Subject: [PATCH 8/9] mkFlake: also pass self.inputs to lib --- lib/mkFlake/default.nix | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/mkFlake/default.nix b/lib/mkFlake/default.nix index ece286ad1..cca6ccdf5 100644 --- a/lib/mkFlake/default.nix +++ b/lib/mkFlake/default.nix @@ -30,7 +30,10 @@ let overlay = cfg.packages; inherit (cfg) overlays; - lib = import "${devos}/lib" { inherit self nixos inputs; }; + lib = import "${devos}/lib" { + inherit self nixos; + inputs = inputs // self.inputs; + }; deploy.nodes = os.mkNodes deploy self.nixosConfigurations; }; From 541bf89cf219952a0f22f24a26697d4f0f919289 Mon Sep 17 00:00:00 2001 From: Pacman99 Date: Thu, 1 Apr 2021 13:50:02 -0700 Subject: [PATCH 9/9] mkHosts: try fix with toString --- lib/devos/mkHosts.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/devos/mkHosts.nix b/lib/devos/mkHosts.nix index e7664012c..e0c7ea477 100644 --- a/lib/devos/mkHosts.nix +++ b/lib/devos/mkHosts.nix @@ -12,7 +12,7 @@ let ]; modules = { - core = "${devos}/profiles/core"; + core = toString ../../profiles/core; modOverrides = { config, overrideModulesPath, ... }: let inherit (overrides) modules disabledModules;