From f35be63f3200410f800d572e337f9b5a6dc95c2f Mon Sep 17 00:00:00 2001 From: guangtao Date: Tue, 3 Oct 2023 22:12:38 -0700 Subject: [PATCH] feat: WIP add nixos example --- examples/nixos/default.nix | 2 +- flake.nix | 7 ++- .../pops/data.nix => lib/loadDataAll.nix | 2 +- lib/readYAML.nix | 20 +++++++ templates/nixos/flake.lock | 21 ++++--- templates/nixos/flake.nix | 32 ++++++++--- .../hosts/nixos => lib}/__lock/flake.lock | 0 .../hosts/nixos => lib}/__lock/flake.nix | 0 templates/nixos/lib/exporters.nix | 55 +++++++++++++++++++ templates/nixos/lib/inputs.nix | 13 +++++ templates/nixos/lib/nixos.nix | 14 ++--- templates/nixos/lib/omnibus.nix | 15 +++++ templates/nixos/nixos/hosts/nixos/default.nix | 43 --------------- templates/nixos/nixos/hosts/nixos/layouts.nix | 45 +++++++++++++++ .../nixos/hosts/nixos/nixosConfiguration.nix | 5 ++ .../nixos/nixos/hosts/nixos/pops/inputs.nix | 5 +- templates/nixos/packages/hello.nix | 50 +++++++++++++++++ 17 files changed, 257 insertions(+), 72 deletions(-) rename templates/nixos/nixos/hosts/nixos/pops/data.nix => lib/loadDataAll.nix (72%) create mode 100644 lib/readYAML.nix rename templates/nixos/{nixos/hosts/nixos => lib}/__lock/flake.lock (100%) rename templates/nixos/{nixos/hosts/nixos => lib}/__lock/flake.nix (100%) create mode 100644 templates/nixos/lib/exporters.nix create mode 100644 templates/nixos/lib/inputs.nix create mode 100644 templates/nixos/lib/omnibus.nix delete mode 100644 templates/nixos/nixos/hosts/nixos/default.nix create mode 100644 templates/nixos/nixos/hosts/nixos/layouts.nix create mode 100644 templates/nixos/nixos/hosts/nixos/nixosConfiguration.nix create mode 100644 templates/nixos/packages/hello.nix diff --git a/examples/nixos/default.nix b/examples/nixos/default.nix index 9b197bf..777a00b 100644 --- a/examples/nixos/default.nix +++ b/examples/nixos/default.nix @@ -19,7 +19,7 @@ in outputs.srvos.default.common.nix - (omnibus.lib.outputs.default.mkHome + (omnibus.lib.mkHome { admin = { uid = 1000; diff --git a/flake.nix b/flake.nix index da19703..cd07c56 100644 --- a/flake.nix +++ b/flake.nix @@ -14,12 +14,17 @@ ... }@inputs: let + omnibus = self.lib // { + lib = self.lib.lib; + }; lib = flops.lib.haumea.pops.default.setInit { src = ./lib; loader = haumea.lib.loaders.scoped; inputs = { lib = flops.inputs.nixlib.lib // builtins; home-manager = self.lib.loadInputs.outputs.home-manager; + haumea = haumea.lib; + inherit omnibus; }; }; @@ -116,7 +121,7 @@ haumea = flops.inputs.haumea.lib; dmerge = flops.inputs.dmerge; POP = POP.lib; - omnibus = self.lib; + inherit omnibus; }; }; }; diff --git a/templates/nixos/nixos/hosts/nixos/pops/data.nix b/lib/loadDataAll.nix similarity index 72% rename from templates/nixos/nixos/hosts/nixos/pops/data.nix rename to lib/loadDataAll.nix index 927ba4a..c8e7af0 100644 --- a/templates/nixos/nixos/hosts/nixos/pops/data.nix +++ b/lib/loadDataAll.nix @@ -2,7 +2,7 @@ loader = with haumea; [ (matchers.regex "^(.+)\\.(yaml|yml)$" ( _: _: path: - super.inputs.outputs.std.lib.ops.readYAML path + super.readYAML path )) ]; }) diff --git a/lib/readYAML.nix b/lib/readYAML.nix new file mode 100644 index 0000000..96ba0e7 --- /dev/null +++ b/lib/readYAML.nix @@ -0,0 +1,20 @@ +{ nixpkgs }: # source from std +let + inherit (builtins) fromJSON; + inherit (nixpkgs) runCommand yq-go; + inherit (nixpkgs.lib) readFile; +in +# Read a YAML file into a Nix datatype using IFD. +# Similar to: +# > builtins.fromJSON (builtins.readFile ./somefile) +# but takes an input file in YAML instead of JSON. +# +# Type: +# Path -> a :: Nix +path: +let + jsonOutputDrv = + runCommand "from-yaml" { nativeBuildInputs = [ yq-go ]; } + ''yq -o=json "${path}" > "$out"''; +in +fromJSON (readFile jsonOutputDrv) diff --git a/templates/nixos/flake.lock b/templates/nixos/flake.lock index 7504f7b..4a6774a 100644 --- a/templates/nixos/flake.lock +++ b/templates/nixos/flake.lock @@ -403,17 +403,16 @@ ] }, "locked": { - "lastModified": 1696323936, - "narHash": "sha256-xEKOV/i0u8sLLpMTbPqJWAoDQKd+BAiueWdSr5NSQao=", - "owner": "gtrunsec", - "repo": "omnibus", - "rev": "e0fa902a57dee4914811817aa7415756b0aa54d6", - "type": "github" + "dirtyRev": "de3129c12f3d258c700ca541e940eff7801f2ea0-dirty", + "dirtyShortRev": "de3129c-dirty", + "lastModified": 1696368797, + "narHash": "sha256-hUhUtFKG7k47YriaQeSOMYOrK5CNs6RlVKZXlyidrvk=", + "type": "git", + "url": "file:///home/guangtao/ghq/github.com/GTrunSec/omnibus" }, "original": { - "owner": "gtrunsec", - "repo": "omnibus", - "type": "github" + "type": "git", + "url": "file:///home/guangtao/ghq/github.com/GTrunSec/omnibus" } }, "root": { @@ -421,6 +420,10 @@ "darwin": "darwin", "darwin-unstable": "darwin-unstable", "disko": "disko", + "flops": [ + "omnibus", + "flops" + ], "haumea": [ "omnibus", "flops", diff --git a/templates/nixos/flake.nix b/templates/nixos/flake.nix index c48554e..3a9b632 100644 --- a/templates/nixos/flake.nix +++ b/templates/nixos/flake.nix @@ -8,8 +8,10 @@ srvos.url = "github:numtide/srvos"; srvos.inputs.nixpkgs.follows = "nixpkgs"; - omnibus.url = "github:gtrunsec/omnibus"; + # omnibus.url = "github:gtrunsec/omnibus"; + omnibus.url = "/home/guangtao/ghq/github.com/GTrunSec/omnibus"; haumea.follows = "omnibus/flops/haumea"; + flops.follows = "omnibus/flops"; }; # nixpkgs & home-manager @@ -36,12 +38,28 @@ outputs = { self, ... }@inputs: + let + eachSystem = inputs.nixpkgs.lib.genAttrs [ + "x86_64-linux" + "x86_64-darwin" + "aarch64-linux" + "aarch64-darwin" + ]; + in { - lib = inputs.omnibus.exporters.addLoadExtender { - src = ./lib; - inputs = { - inherit inputs; - }; - }; + lib = + (inputs.omnibus.exporters.addLoadExtender { + src = ./lib; + inputs = { + inherit inputs eachSystem; + }; + }).outputs.default; + + inherit (self.lib.exporters) + darwinConfigurations + nixosConfigurations + packages + local + ; }; } diff --git a/templates/nixos/nixos/hosts/nixos/__lock/flake.lock b/templates/nixos/lib/__lock/flake.lock similarity index 100% rename from templates/nixos/nixos/hosts/nixos/__lock/flake.lock rename to templates/nixos/lib/__lock/flake.lock diff --git a/templates/nixos/nixos/hosts/nixos/__lock/flake.nix b/templates/nixos/lib/__lock/flake.nix similarity index 100% rename from templates/nixos/nixos/hosts/nixos/__lock/flake.nix rename to templates/nixos/lib/__lock/flake.nix diff --git a/templates/nixos/lib/exporters.nix b/templates/nixos/lib/exporters.nix new file mode 100644 index 0000000..08b071e --- /dev/null +++ b/templates/nixos/lib/exporters.nix @@ -0,0 +1,55 @@ +let + filterConfigs = + config: + lib.pipe self.hosts [ + (lib.filterAttrs (_: v: v ? "${config}")) + (lib.mapAttrs (_: v: v.${config})) + ]; +in +{ + hosts = + (inputs.omnibus.exporters.addLoadExtender { + src = ../nixos/hosts; + inputs = inputs // { + self' = { + inherit (super) inputs; + }; + omnibus = inputs.omnibus.lib // { + lib = super.omnibus.lib.outputs.default; + }; + }; + }).outputs.default; + + nixosConfigurations = filterConfigs "nixosConfiguration"; + + darwinConfigurations = filterConfigs "darwinConfiguration"; + + local = eachSystem ( + system: + let + inputs = (super.inputs.setSystem system).outputs; + loadDataAll = + (omnibus.lib.addLoadExtender { + inputs = { + nixpkgs = inputs.nixpkgs.legacyPackages.${system}; + }; + }).outputs.default.loadDataAll; + in + { + data = (loadDataAll.addLoadExtender { src = ../local/data; }).outputs.default; + } + ); + + packages = eachSystem ( + system: + let + inputs = (super.inputs.setSystem system).outputs; + in + ( + (flops.haumea.pops.default.setInit { + src = ../packages; + loader = _: path: inputs.nixpkgs.callPackage path { }; + }) + ).outputs.default + ); +} diff --git a/templates/nixos/lib/inputs.nix b/templates/nixos/lib/inputs.nix new file mode 100644 index 0000000..c9e1d74 --- /dev/null +++ b/templates/nixos/lib/inputs.nix @@ -0,0 +1,13 @@ +(omnibus.loadInputs.addInputsExtender ( + POP.extendPop flops.flake.pops.inputsExtender ( + self: super: + let + selfInputs = omnibus.loadInputs.setInitInputs ./__lock; + in + { + inputs = { + nixpkgs = inputs.nixpkgs.legacyPackages; + } // selfInputs.outputs; + } + ) +)) diff --git a/templates/nixos/lib/nixos.nix b/templates/nixos/lib/nixos.nix index 6865230..b95b53f 100644 --- a/templates/nixos/lib/nixos.nix +++ b/templates/nixos/lib/nixos.nix @@ -1,19 +1,19 @@ { loadHomeModules = (omnibus.loadHomeModules.addLoadExtender { src = ../nixos/homeModules; }); + loadHomeProfiles = (omnibus.loadHomeProfiles.addLoadExtender { src = ../nixos/homeProfiles; }); loadNixOSProfiles = (omnibus.loadNixOSProfiles.addLoadExtender { src = ../nixos/nixosProfiles; }); + loadNixOSModules = (omnibus.loadNixOSModules.addLoadExtender { src = ../nixos/nixosModules; }); - omnibus = { - lib = omnibus.lib.addLoadExtender { - inputs = { - home-manager = inputs.home; - }; - }; - }; + loadDarwinProfiles = + (omnibus.loadNixOSProfiles.addLoadExtender { src = ../nixos/darwinProfiles; }); + + loadDarwinModules = + (omnibus.loadNixOSModules.addLoadExtender { src = ../nixos/darwinModules; }); } diff --git a/templates/nixos/lib/omnibus.nix b/templates/nixos/lib/omnibus.nix new file mode 100644 index 0000000..ad1d1ae --- /dev/null +++ b/templates/nixos/lib/omnibus.nix @@ -0,0 +1,15 @@ +{ + loadHomeModules = (omnibus.loadHomeModules.addLoadExtender { }); + + loadHomeProfiles = (omnibus.loadHomeProfiles.addLoadExtender { }); + + loadNixOSProfiles = (omnibus.loadNixOSProfiles.addLoadExtender { }); + + loadNixOSModules = (omnibus.loadNixOSModules.addLoadExtender { }); + + lib = omnibus.lib.addLoadExtender { + inputs = { + home-manager = inputs.home; + }; + }; +} diff --git a/templates/nixos/nixos/hosts/nixos/default.nix b/templates/nixos/nixos/hosts/nixos/default.nix deleted file mode 100644 index 777a00b..0000000 --- a/templates/nixos/nixos/hosts/nixos/default.nix +++ /dev/null @@ -1,43 +0,0 @@ -let - outputs = lib.mapAttrs (_: v: v.outputs) ( - lib.removeAttrs super.pops [ "inputs" ] - ); -in -{ - data = outputs.data.default; - - nixosSuites = lib.flatten [ - outputs.selfNixOSProfiles.default.bootstrap - - # self.nixosProfiles.default.presets.boot - outputs.nixosModules.default.programs.git - - # # --custom profiles - outputs.nixosProfiles.customProfiles.presets.nix - outputs.nixosProfiles.customProfiles.presets.boot - outputs.nixosModules.customModules.boot - - outputs.srvos.default.common.nix - - (omnibus.lib.mkHome - { - admin = { - uid = 1000; - description = "default manager"; - isNormalUser = true; - extraGroups = [ "wheel" ]; - }; - } - "zsh" - self.homeSuites - ) - ]; - - homeSuites = [ - outputs.homeProfiles.customProfiles.presets.hyprland - outputs.homeProfiles.default.presets.bat - # # The parent directory of "presets" is categorized as a list type of "suites" - (outputs.homeProfiles.default.shell { }).default - # super.pops.homeModules.outputs.default.wayland.windowManager.hyprland - ]; -} diff --git a/templates/nixos/nixos/hosts/nixos/layouts.nix b/templates/nixos/nixos/hosts/nixos/layouts.nix new file mode 100644 index 0000000..ef9b4a6 --- /dev/null +++ b/templates/nixos/nixos/hosts/nixos/layouts.nix @@ -0,0 +1,45 @@ +let + outputs = lib.mapAttrs (_: v: v.outputs) ( + lib.removeAttrs super.pops [ "inputs" ] + ); +in +{ + system = "x86_64-linux"; + + data = outputs.data.default; + + nixosSuites = lib.flatten [ + # outputs.selfNixOSProfiles.default.bootstrap + + # # self.nixosProfiles.default.presets.boot + # outputs.nixosModules.default.programs.git + + # # # --custom profiles + # outputs.nixosProfiles.customProfiles.presets.nix + # outputs.nixosProfiles.customProfiles.presets.boot + # outputs.nixosModules.customModules.boot + + # outputs.srvos.default.common.nix + (omnibus.lib.mkHome + { + admin = { + uid = 1000; + description = "default manager"; + isNormalUser = true; + extraGroups = [ "wheel" ]; + }; + } + "zsh" + self.homeSuites + ) + ]; + + homeSuites = + [ + # outputs.homeProfiles.customProfiles.presets.hyprland + # outputs.homeProfiles.default.presets.bat + # # # The parent directory of "presets" is categorized as a list type of "suites" + # (outputs.homeProfiles.default.shell { }).default + # super.pops.homeModules.outputs.default.wayland.windowManager.hyprland + ]; +} diff --git a/templates/nixos/nixos/hosts/nixos/nixosConfiguration.nix b/templates/nixos/nixos/hosts/nixos/nixosConfiguration.nix new file mode 100644 index 0000000..1dd2492 --- /dev/null +++ b/templates/nixos/nixos/hosts/nixos/nixosConfiguration.nix @@ -0,0 +1,5 @@ +nixpkgs.lib.nixosSystem rec { + system = super.layouts.system; + pkgs = import nixpkgs { inherit system; }; + modules = lib.flatten [ super.layouts.nixosSuites ]; +} diff --git a/templates/nixos/nixos/hosts/nixos/pops/inputs.nix b/templates/nixos/nixos/hosts/nixos/pops/inputs.nix index ba80cd3..6e54b7a 100644 --- a/templates/nixos/nixos/hosts/nixos/pops/inputs.nix +++ b/templates/nixos/nixos/hosts/nixos/pops/inputs.nix @@ -1,9 +1,8 @@ -(omnibus.loadInputs.addInputsExtender ( +(self'.inputs.loadInputs.addInputsExtender ( POP.extendPop flops.flake.pops.inputsExtender ( self: super: let selfInputs = omnibus.loadInputs.setInitInputs ../__lock; - local = omnibus.loadInputs.setInitInputs ../../../local; in { inputs = { @@ -13,4 +12,4 @@ } ) )).setSystem - "x86_64-linux" + root.nixos.layouts.system diff --git a/templates/nixos/packages/hello.nix b/templates/nixos/packages/hello.nix new file mode 100644 index 0000000..59fc906 --- /dev/null +++ b/templates/nixos/packages/hello.nix @@ -0,0 +1,50 @@ +{ + callPackage, + lib, + stdenv, + fetchurl, + nixos, + testers, + hello, +}: + +stdenv.mkDerivation ( + finalAttrs: { + pname = "hello"; + version = "2.12.1"; + + src = fetchurl { + url = "mirror://gnu/hello/hello-${finalAttrs.version}.tar.gz"; + sha256 = "sha256-jZkUKv2SV28wsM18tCqNxoCZmLxdYH2Idh9RLibH2yA="; + }; + + doCheck = true; + + passthru.tests = { + version = testers.testVersion { package = hello; }; + + invariant-under-noXlibs = + testers.testEqualDerivation + "hello must not be rebuilt when environment.noXlibs is set." + hello + (nixos { environment.noXlibs = true; }).pkgs.hello; + }; + + passthru.tests.run = callPackage ./test.nix { + hello = finalAttrs.finalPackage; + }; + + meta = with lib; { + description = "A program that produces a familiar, friendly greeting"; + longDescription = '' + GNU Hello is a program that prints "Hello, world!" when you run it. + It is fully customizable. + ''; + homepage = "https://www.gnu.org/software/hello/manual/"; + changelog = "https://git.savannah.gnu.org/cgit/hello.git/plain/NEWS?h=v${finalAttrs.version}"; + license = licenses.gpl3Plus; + maintainers = [ maintainers.eelco ]; + platforms = platforms.all; + }; + } +)