From 165191391025e956fd8349606663e51014753e10 Mon Sep 17 00:00:00 2001 From: Timothy DeHerrera Date: Thu, 25 Feb 2021 14:47:19 -0700 Subject: [PATCH] profiles: simplify profiles to suites * Leave importing to nixpkgs module implentation. Provide a path instead; resolves #136. * Allow profiles which are not lambdas but simple attribute sets, relaxing the constraints a bit. * Update profile README.md * defaultImports -> mkProfileAttrs: allow importing subprofiles even if parent directory does not contain a default.nix. --- hosts/NixOS.nix | 2 +- hosts/default.nix | 2 +- lib/default.nix | 18 ++++++++++-------- profiles/README.md | 35 ++++++++++++++++++++++++++++------- profiles/user | 1 - suites/default.nix | 20 ++++++++++---------- 6 files changed, 50 insertions(+), 28 deletions(-) delete mode 120000 profiles/user diff --git a/hosts/NixOS.nix b/hosts/NixOS.nix index 15c2a3c0..3b084119 100644 --- a/hosts/NixOS.nix +++ b/hosts/NixOS.nix @@ -1,7 +1,7 @@ { suites, ... }: { ### root password is empty by default ### - imports = suites.core; + imports = suites.base; boot.loader.systemd-boot.enable = true; boot.loader.efi.canTouchEfiVariables = true; diff --git a/hosts/default.nix b/hosts/default.nix index 6bc06e4d..864c466b 100644 --- a/hosts/default.nix +++ b/hosts/default.nix @@ -23,7 +23,7 @@ let modules = let - core = import ../profiles/core; + core = ../profiles/core; modOverrides = { config, overrideModulesPath, ... }: let diff --git a/lib/default.nix b/lib/default.nix index de395e21..5e3185a3 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -4,7 +4,7 @@ let pathExists filter; inherit (nixos.lib) fold filterAttrs hasSuffix mapAttrs' nameValuePair removeSuffix - recursiveUpdate genAttrs nixosSystem mkForce; + recursiveUpdate genAttrs nixosSystem mkForce optionalAttrs; # mapFilterAttrs :: # (name -> value -> bool ) @@ -58,15 +58,15 @@ let }); /** - Synopsis: importDefaults _path_ + Synopsis: mkProfileAttrs _path_ Recursively import the subdirs of _path_ containing a default.nix. Example: - let profiles = importDefaults ./profiles; in + let profiles = mkProfileAttrs ./profiles; in assert profiles ? core.default; 0 **/ - importDefaults = dir: + mkProfileAttrs = dir: let imports = let @@ -74,19 +74,21 @@ let p = n: v: v == "directory" - && pathExists "${dir}/${n}/default.nix"; + && n != "profiles"; in filterAttrs p files; f = n: _: - { default = import "${dir}/${n}/default.nix"; } - // importDefaults "${dir}/${n}"; + optionalAttrs + (pathExists "${dir}/${n}/default.nix") + { default = "${dir}/${n}"; } + // mkProfileAttrs "${dir}/${n}"; in mapAttrs f imports; in { - inherit importDefaults mapFilterAttrs genAttrs' pkgImport + inherit mkProfileAttrs mapFilterAttrs genAttrs' pkgImport pathsToImportedAttrs mkNodes; overlays = pathsToImportedAttrs overlayPaths; diff --git a/profiles/README.md b/profiles/README.md index 1e17c2a1..50157a8c 100644 --- a/profiles/README.md +++ b/profiles/README.md @@ -5,14 +5,16 @@ window manager. If you need some concrete examples, just checkout the community [branch](https://github.com/divnix/devos/tree/community/profiles). ## Constraints -For the sake of consistency, there are a few minor constraints. First of all, a -profile should always be defined in a `default.nix`, and it should always be a -a function taking a single attribute set as an argument, and returning a NixOS -module which does not define any new module options. If you need to make new -module option declarations, just use [modules](../modules). +For the sake of consistency, a profile should always be defined in a +_default.nix_ containing a valid [nixos module](https://nixos.wiki/wiki/Module) +which ___does not___ declare any new +[module options](https://nixos.org/manual/nixos/stable/index.html#sec-option-declarations). +If you need to do that, use the [modules directory](../modules). -These restrictions help simplify the import logic used to pass profles to -[suites](../suites). +> ##### _Note:_ +> [hercules-ci](../doc/integrations/hercules.md) expects all profiles to be +> defined in a _default.nix_. Similarly, [suites](../suites) expect a +> _default.nix_ as well. ### Example #### Correct ✔ @@ -40,6 +42,25 @@ program configurations such as your text editor, and shell configs. This way, you can either pull in the whole development profile, or pick and choose individual programs. +### Example + +profiles/develop/default.nix: +```nix +{ + imports = [ ./zsh ]; + # some generic development concerns ... +} +``` + +profiles/develop/zsh/default.nix: +```nix +{ ... }: +{ + programs.zsh.enable = true; + # zsh specific options ... +} +``` + ## Conclusion Profiles are the most important concept in devos. They allow us to keep our nix expressions self contained and modular. This way we can maximize reuse diff --git a/profiles/user b/profiles/user deleted file mode 120000 index eb25cfc1..00000000 --- a/profiles/user +++ /dev/null @@ -1 +0,0 @@ -../users/profiles \ No newline at end of file diff --git a/suites/default.nix b/suites/default.nix index 016e8e37..7b94a83a 100644 --- a/suites/default.nix +++ b/suites/default.nix @@ -1,24 +1,24 @@ { lib }: let inherit (builtins) mapAttrs isFunction; - inherit (lib.flk) importDefaults; + inherit (lib.flk) mkProfileAttrs profileMap; - profiles = importDefaults (toString ../profiles); - users = importDefaults (toString ../users); + profiles = mkProfileAttrs (toString ../profiles); + users = mkProfileAttrs (toString ../users); allProfiles = - let - sansCore = lib.filterAttrs (n: _: n != "core") profiles; - in - lib.collect isFunction sansCore; + let defaults = lib.collect (x: x ? default) profiles; + in map (x: x.default) defaults; - allUsers = lib.collect isFunction users; + allUsers = + let defaults = lib.collect (x: x ? default) users; + in map (x: x.default) defaults; suites = with profiles; rec { - core = [ users.nixos users.root ]; + base = [ users.nixos users.root ]; }; in -mapAttrs (_: v: lib.flk.profileMap v) suites // { +mapAttrs (_: v: profileMap v) suites // { inherit allProfiles allUsers; }