Modular, context-aware and aspect-oriented dendritic Nix configurations. Discussions: https://oeiuwq.zulipchat.com/join/nqp26cd4kngon6mo3ncgnuap/ den.oeiuwq.com
configurations den dendritic nix aspect oriented

feat: templates/noflake apps per host to build with nh (#227)

authored by oeiuwq.com and committed by

GitHub 24731d28 4b3f7765

+114 -56
+3 -1
.github/workflows/test.yml
··· 61 61 needs: [approved] 62 62 name: noflake 63 63 runs-on: ubuntu-latest 64 + env: 65 + NIX_PATH: "nixpkgs=https://github.com/NixOS/nixpkgs/archive/nixos-unstable.tar.gz" 64 66 steps: 65 67 - uses: wimpysworld/nothing-but-nix@main 66 68 - uses: cachix/install-nix-action@v31 ··· 74 76 } 75 77 EOF 76 78 git add templates/noflake/modules/ci-runtime.nix 77 - - run: cd templates/noflake && nix-build -A flake.nixosConfigurations.igloo.config.system.build.toplevel 79 + - run: cd templates/noflake && nix-shell . -A den.sh --run 'igloo build' 78 80 minimal: 79 81 needs: [approved] 80 82 name: minimal
+10 -52
modules/output.nix
··· 1 1 { inputs, lib, ... }: 2 - if inputs ? flake-parts then 3 - { } 4 - else 5 - { 6 - # NOTE: Currently Den needs a top-level attribute where to place configurations, 7 - # by default it is the `flake` attribute, even if Den uses no flake-parts at all. 8 - 9 - # This definitions has been adapted from https://github.com/hercules-ci/flake-parts 10 - # project which is licensed with: 11 - # 12 - # MIT License 13 - # 14 - # Copyright (c) 2021 Hercules CI 15 - # 16 - # Permission is hereby granted, free of charge, to any person obtaining a copy 17 - # of this software and associated documentation files (the "Software"), to deal 18 - # in the Software without restriction, including without limitation the rights 19 - # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 - # copies of the Software, and to permit persons to whom the Software is 21 - # furnished to do so, subject to the following conditions: 22 - # 23 - # The above copyright notice and this permission notice shall be included in all 24 - # copies or substantial portions of the Software. 25 - # 26 - # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 - # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 - # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 - # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 - # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 - # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 - # SOFTWARE. 33 - options.flake = lib.mkOption { 34 - type = lib.types.submoduleWith { 35 - modules = [ 36 - { 37 - freeformType = lib.types.lazyAttrsOf ( 38 - lib.types.unique { 39 - message = '' 40 - No option has been declared for this flake output attribute, so its definitions can't be merged automatically. 41 - Possible solutions: 42 - - Load a module that defines this flake output attribute 43 - - Declare an option for this flake output attribute 44 - - Make sure the output attribute is spelled correctly 45 - - Define the value only once, with a single definition in a single module 46 - ''; 47 - } lib.types.raw 48 - ); 49 - } 50 - ]; 51 - }; 52 - }; 53 - } 2 + let 3 + has-flake-parts = inputs ? flake-parts; 4 + outputOptions.flake = lib.mkOption { 5 + default = { }; 6 + type = lib.types.submodule { freeformType = lib.types.lazyAttrsOf lib.types.unspecified; }; 7 + }; 8 + in 9 + { 10 + options = lib.optionalAttrs (!has-flake-parts) outputOptions; 11 + }
+12
templates/noflake/README.md
··· 12 12 npins update den # make sure you use latest Den 13 13 ``` 14 14 15 + This template provides runnable apps for each host/home (see modules/nh.nix) 16 + 17 + ```shell 18 + # default action is `build` 19 + nix-run . -A den.sh --run igloo 20 + 21 + # you can specify any other action like `switch`, `repl`, `build`, `--help` 22 + nix-run . -A den.sh --run 'igloo build' 23 + ``` 24 + 25 + or 26 + 15 27 ```shell 16 28 nixos-rebuild build --file . -A flake.nixosConfigurations.igloo 17 29 ```
+18 -3
templates/noflake/modules/den.nix
··· 1 - { inputs, ... }: 1 + { den, inputs, ... }: 2 2 { 3 3 # we can import this flakeModule even if we dont have flake-parts as input! 4 4 imports = [ inputs.den.flakeModule ]; ··· 11 11 12 12 # tux user on igloo host, using nix-maid 13 13 den.hosts.x86_64-linux.igloo.users.tux.classes = [ "maid" ]; 14 + # tux on iceberg host with NixOS user environment 15 + den.hosts.x86_64-linux.iceberg.users.tux.classes = [ "user" ]; 16 + 17 + # first: `npins add -n darwin github nix-darwin nix-darwin` 18 + # den.hosts.aarch64-darwin.apple.users.tux.classes = [ "hjem" ]; 14 19 15 20 # host aspect 16 21 den.aspects.igloo = { ··· 25 30 26 31 # user aspect 27 32 den.aspects.tux = { 28 - # user configures host nixos.users.users.tux.isNormalUser. 33 + # den batteries or your own re-usable aspects 34 + includes = [ den.provides.define-user ]; 35 + 36 + # user configures host <nixos/darwin>.users.users.tux.description 29 37 # Read docs about the `user` class. 30 - user.isNormalUser = true; 38 + user.description = "Cute Penguin"; 39 + 40 + # user contributes nixos and darwin common config 41 + os = 42 + { pkgs, ... }: 43 + { 44 + environment.systemPackages = [ pkgs.hello ]; 45 + }; 31 46 32 47 # maid class 33 48 maid.file.home.".gitconfig".text = ''
+71
templates/noflake/modules/nh.nix
··· 1 + # Provides shell utilities under `den.sh` for building OS configurations using 2 + # github:nix-community/nh instead of nixos-rebuild, etc 3 + { lib, den, ... }: 4 + let 5 + defaultAction = "build"; 6 + 7 + denShell = 8 + pkgs: 9 + pkgs.mkShell { 10 + buildInputs = [ pkgs.nh ] ++ (denApps pkgs); 11 + }; 12 + 13 + hosts = lib.concatMap lib.attrValues (lib.attrValues den.hosts); 14 + homes = lib.concatMap lib.attrValues (lib.attrValues den.homes); 15 + 16 + denApps = pkgs: (map (os pkgs) hosts) ++ (map (hm pkgs) homes); 17 + 18 + os = 19 + pkgs: host: 20 + pkgs.writeShellApplication { 21 + name = host.name; 22 + runtimeInputs = [ pkgs.nh ]; 23 + text = 24 + let 25 + command = 26 + { 27 + darwin = "darwin"; 28 + nixos = "os"; 29 + } 30 + .${host.class}; 31 + attr = lib.concatStringsSep "." ([ "flake" ] ++ host.intoAttr); 32 + args = lib.concatStringsSep " " [ 33 + "--file" 34 + "." 35 + attr 36 + ]; 37 + in 38 + '' 39 + action="''${1:-${defaultAction}}" 40 + shift || true 41 + exec nh ${command} "$action" ${args} "$@" 42 + ''; 43 + }; 44 + 45 + hm = 46 + pkgs: home: 47 + pkgs.writeShellApplication { 48 + name = home.name; 49 + runtimeInputs = [ pkgs.nh ]; 50 + text = 51 + let 52 + attr = lib.concatStringsSep "." ([ "flake" ] ++ home.intoAttr); 53 + args = lib.concatStringsSep " " [ 54 + "--file" 55 + "." 56 + attr 57 + ]; 58 + in 59 + '' 60 + action="''${1:-${defaultAction}}" 61 + shift || true 62 + exec nh home "$action" ${args} "$@" 63 + ''; 64 + }; 65 + in 66 + { 67 + options.den.sh = lib.mkOption { 68 + description = "Non-flake Den shell environment"; 69 + default = denShell (import <nixpkgs> { }); 70 + }; 71 + }