From 2898c47279ceacd48c8b38e558708b2a09deb2aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20J=C3=A4ckel?= Date: Mon, 8 Jan 2024 00:39:51 +0100 Subject: [PATCH] Add docs --- .github/workflows/gh-pages.yaml | 44 +++++++++++++++++++++++++++ flake.lock | 53 +++++++++++++++++++++++++++------ flake.nix | 47 +++++++++++++++++++---------- lib/doc.nix | 27 +++++++++++++++++ 4 files changed, 147 insertions(+), 24 deletions(-) create mode 100644 .github/workflows/gh-pages.yaml create mode 100644 lib/doc.nix diff --git a/.github/workflows/gh-pages.yaml b/.github/workflows/gh-pages.yaml new file mode 100644 index 00000000..49fda71 --- /dev/null +++ b/.github/workflows/gh-pages.yaml @@ -0,0 +1,44 @@ +name: "pages" +on: + push: + branches: + - master + workflow_dispatch: + +permissions: + contents: read + pages: write + id-token: write + +concurrency: + group: "pages" + cancel-in-progress: false + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Install Nix + uses: cachix/install-nix-action@v24 + + - name: Build docs + run: nix build .#docbook + + - name: Prepare assets for upload + run: cp -r --dereference --no-preserve=mode,ownership result/ public/ + + - name: Upload artifact + uses: actions/upload-pages-artifact@v2 + with: + path: public/ + + deploy: + runs-on: ubuntu-latest + needs: build + steps: + - name: Deploy to GitHub Pages + id: deployment + uses: actions/deploy-pages@v3 diff --git a/flake.lock b/flake.lock index 1e29214..385a61c 100644 --- a/flake.lock +++ b/flake.lock @@ -1,23 +1,58 @@ { "nodes": { - "nixpkgs-lib": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, "locked": { - "lastModified": 1701564385, - "narHash": "sha256-um5ce7hnsQ8Do+oKf90zGKVmEqufr4Q6T8zfY9Hon38=", - "owner": "nix-community", - "repo": "nixpkgs.lib", - "rev": "152c00fc19bc45af5dd65bd41d1d020c2ba0b4ca", + "lastModified": 1701680307, + "narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "4022d587cbbfd70fe950c1e2083a02621806a725", "type": "github" }, "original": { - "owner": "nix-community", - "repo": "nixpkgs.lib", + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1704194953, + "narHash": "sha256-RtDKd8Mynhe5CFnVT8s0/0yqtWFMM9LmCzXv/YKxnq4=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "bd645e8668ec6612439a9ee7e71f7eac4099d4f6", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", "type": "github" } }, "root": { "inputs": { - "nixpkgs-lib": "nixpkgs-lib" + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" } } }, diff --git a/flake.nix b/flake.nix index 517d46f..8017a2b 100644 --- a/flake.nix +++ b/flake.nix @@ -2,15 +2,17 @@ description = "Opinionated shared nixos configurations"; inputs = { - nixpkgs-lib.url = "github:nix-community/nixpkgs.lib"; + flake-utils.url = "github:numtide/flake-utils"; + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; }; - outputs = { self, nixpkgs-lib, ... }: + outputs = { self, flake-utils, nixpkgs, ... }: let - inherit (nixpkgs-lib) lib; + inherit (nixpkgs) lib; src = builtins.filterSource (path: type: type == "directory" || lib.hasSuffix ".nix" (baseNameOf path)) ./.; ls = dir: lib.attrNames (builtins.readDir (src + "/${dir}")); fileList = dir: map (file: ./. + "/${dir}/${file}") (ls dir); + importDirToKey = dir: args: lib.listToAttrs (map (file: { name = lib.removeSuffix ".nix" file; @@ -18,6 +20,10 @@ }) (ls dir) ); + + importLibS = args: { + _module.args.libS = lib.mkOverride 1000 (self.lib args); + }; in { lib = args: @@ -25,24 +31,35 @@ lib' = importDirToKey "lib" args; in lib' // { + # some functions get promoted to be directly under libS + inherit (lib'.doc) mkModuleDoc; inherit (lib'.modules) mkOpinionatedOption mkRecursiveDefault; inherit (lib'.ssh) mkPubKey; }; nixosModules = lib.foldr (a: b: a // b) { } (map - ( - name: { - "${lib.removeSuffix ".nix" name}" = { - imports = [ ./modules/${name} ]; + (name: { + "${lib.removeSuffix ".nix" name}" = { config, lib, pkgs, ... }: + (importLibS { inherit config lib pkgs; }) // { + imports = [ + ./modules + ./modules/${name} + ]; }; - } - ) - (ls "modules")); + }) + (ls "modules") + ); - nixosModule = { config, ... }: { - _module.args.libS = lib.mkOverride 1000 (self.lib { inherit lib config; }); - - imports = fileList "modules"; + nixosModule = { config, lib, pkgs, ... }: + (importLibS { inherit config lib pkgs; }) // { + imports = fileList "modules"; + }; + } // flake-utils.lib.eachDefaultSystem (system: let + libS = self.lib { config = { }; inherit lib; pkgs = nixpkgs.legacyPackages.${system}; }; + in { + packages.options-doc = libS.mkModuleDoc { + module = self.nixosModule; + urlPrefix = "https://github.com/SuperSandro2000/nixos-modules/tree/master/"; }; - }; + }); } diff --git a/lib/doc.nix b/lib/doc.nix new file mode 100644 index 00000000..597073a --- /dev/null +++ b/lib/doc.nix @@ -0,0 +1,27 @@ +{ lib, pkgs, ... }: + +{ + # based on https://github.com/j-brn/nixos-vfio/blob/master/lib/mkModuleDoc.nix + mkModuleDoc = { module, urlPrefix }: let + inherit (lib.evalModules { + modules = [ { + config._module.check = false; + } module ]; + }) options; + filteredOptions = lib.filterAttrs (key: _: key != "_module") options; + docs = pkgs.nixosOptionsDoc { + options = filteredOptions; + warningsAreErrors = false; + }; + url = lib.escape [ ":" "." "-" ] urlPrefix; + in pkgs.runCommand "options.md" { } /* bash */ '' + mkdir $out + cat ${docs.optionsCommonMark} \ + | sed -r -e 's|\[/nix/store/.+\-source/(.+\.nix)\]|[\1]|g' \ + -e 's|\[/nix/store/.+\-source/(.+)\]|[\1/default\.nix]|g' \ + -e 's|\[flake\\.nix\\#nixosModules\\.(\w+)\/default\.nix\]|\[modules\/\1\/default\.nix\]|g' \ + -e 's|file\:///nix/store/.+\-source/(.+\.nix)|${url}\1|g' \ + -e 's|file\:///nix/store/.+\-source/(.+)\)|${url}/\1/default\.nix\)|g' \ + > $out/options.md + ''; +}