diff --git a/.envrc b/.envrc index 17a257a..b740be0 100644 --- a/.envrc +++ b/.envrc @@ -1,6 +1,10 @@ dotenv + if ! has nix_direnv_version || ! nix_direnv_version 3.0.4; then source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.4/direnvrc" "sha256-DzlYZ33mWF/Gs8DDeyjr8mnVmQGx7ASYqA5WlxwvBG4=" fi + +watch_file flake.nix watch_file nix/devshell.nix + use flake diff --git a/.github/workflows/gh-pages.yml b/.github/workflows/gh-pages.yml index e53d98e..1757b6a 100644 --- a/.github/workflows/gh-pages.yml +++ b/.github/workflows/gh-pages.yml @@ -27,7 +27,7 @@ jobs: - name: Build the docs id: build run: | - nix build .#docs + nix build .#treefmt-docs echo "result=$(readlink ./result)" >> $GITHUB_OUTPUT - name: Upload artifact uses: actions/upload-pages-artifact@v3 diff --git a/default.nix b/default.nix index e3a6c5e..17fcb89 100644 --- a/default.nix +++ b/default.nix @@ -2,7 +2,13 @@ {system ? builtins.currentSystem}: let lock = builtins.fromJSON (builtins.readFile ./flake.lock); - inherit (lock.nodes.flake-compat.locked) owner repo rev narHash; + inherit + (lock.nodes.flake-compat.locked) + owner + repo + rev + narHash + ; flake-compat = fetchTarball { url = "https://github.com/${owner}/${repo}/archive/${rev}.tar.gz"; diff --git a/flake.lock b/flake.lock index 579f642..31d8a4d 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,26 @@ { "nodes": { + "blueprint": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ], + "systems": "systems" + }, + "locked": { + "lastModified": 1719507605, + "narHash": "sha256-Jh/loHiHEkAPBTJv00s1YcQBO2JStioSe3C5BnCB+QA=", + "owner": "numtide", + "repo": "blueprint", + "rev": "16b382c2b52c9c0b15ac4909b8a8ac712f0fc868", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "blueprint", + "type": "github" + } + }, "devshell": { "inputs": { "flake-utils": "flake-utils", @@ -36,29 +57,9 @@ "type": "github" } }, - "flake-parts": { - "inputs": { - "nixpkgs-lib": [ - "nixpkgs" - ] - }, - "locked": { - "lastModified": 1712014858, - "narHash": "sha256-sB4SWl2lX95bExY2gMFG5HIzvva5AVMJd4Igm+GpZNw=", - "owner": "hercules-ci", - "repo": "flake-parts", - "rev": "9126214d0a59633752a136528f5f3b9aa8565b7d", - "type": "github" - }, - "original": { - "owner": "hercules-ci", - "repo": "flake-parts", - "type": "github" - } - }, "flake-utils": { "inputs": { - "systems": "systems" + "systems": "systems_2" }, "locked": { "lastModified": 1701680307, @@ -151,9 +152,9 @@ }, "root": { "inputs": { + "blueprint": "blueprint", "devshell": "devshell", "flake-compat": "flake-compat", - "flake-parts": "flake-parts", "gomod2nix": "gomod2nix", "nix-filter": "nix-filter", "nix-github-actions": "nix-github-actions", @@ -176,6 +177,21 @@ "type": "github" } }, + "systems_2": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, "treefmt-nix": { "inputs": { "nixpkgs": [ diff --git a/flake.nix b/flake.nix index 15eee8d..dc54810 100644 --- a/flake.nix +++ b/flake.nix @@ -2,22 +2,13 @@ description = "Treefmt: once CLI to format your repo"; nixConfig = { - extra-substituters = [ - "https://numtide.cachix.org" - ]; - extra-trusted-public-keys = [ - "numtide.cachix.org-1:2ps1kLBUWjxIneOy1Ik6cQjb41X0iXVXeHigGmycPPE=" - ]; + extra-substituters = ["https://numtide.cachix.org"]; + extra-trusted-public-keys = ["numtide.cachix.org-1:2ps1kLBUWjxIneOy1Ik6cQjb41X0iXVXeHigGmycPPE="]; }; inputs = { - nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; - flake-parts = { - url = "github:hercules-ci/flake-parts"; - inputs.nixpkgs-lib.follows = "nixpkgs"; - }; - treefmt-nix = { - url = "github:numtide/treefmt-nix"; + blueprint = { + url = "github:numtide/blueprint"; inputs.nixpkgs.follows = "nixpkgs"; }; devshell = { @@ -29,28 +20,37 @@ inputs.nixpkgs.follows = "nixpkgs"; inputs.flake-utils.follows = "devshell/flake-utils"; }; - nix-filter.url = "github:numtide/nix-filter"; flake-compat.url = "github:nix-community/flake-compat"; + nix-filter.url = "github:numtide/nix-filter"; nix-github-actions = { url = "github:nix-community/nix-github-actions"; inputs.nixpkgs.follows = "nixpkgs"; }; + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + treefmt-nix = { + url = "github:numtide/treefmt-nix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; }; - outputs = inputs @ {flake-parts, ...}: - flake-parts.lib.mkFlake - { + outputs = inputs: let + inherit (inputs.nixpkgs) lib; + in + inputs.blueprint { inherit inputs; + prefix = "nix/"; + nixpkgs.config = { + allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) ["terraform"]; + }; } - { - imports = [ - ./nix - ]; - systems = [ - "x86_64-linux" - "aarch64-linux" - "x86_64-darwin" - "aarch64-darwin" - ]; + // { + githubActions = inputs.nix-github-actions.lib.mkGithubMatrix { + checks = + lib.getAttrs [ + "x86_64-linux" + "x86_64-darwin" + ] + inputs.self.checks; + }; }; } diff --git a/nix/checks.nix b/nix/checks.nix deleted file mode 100644 index 500f224..0000000 --- a/nix/checks.nix +++ /dev/null @@ -1,16 +0,0 @@ -{ - lib, - inputs, - self, - ... -}: { - # generate github actions matrix using the flake's checks - flake.githubActions = inputs.nix-github-actions.lib.mkGithubMatrix { - checks = lib.getAttrs ["x86_64-linux" "x86_64-darwin"] self.checks; - }; - - perSystem = {self', ...}: { - # mixin every package - checks = with lib; mapAttrs' (n: nameValuePair "package-${n}") self'.packages; - }; -} diff --git a/nix/default.nix b/nix/default.nix deleted file mode 100644 index 809fa71..0000000 --- a/nix/default.nix +++ /dev/null @@ -1,10 +0,0 @@ -{...}: { - imports = [ - ./checks.nix - ./devshell.nix - ./docs.nix - ./nixpkgs.nix - ./packages.nix - ./treefmt.nix - ]; -} diff --git a/nix/devshell.nix b/nix/devshell.nix index 1904f38..bbd7607 100644 --- a/nix/devshell.nix +++ b/nix/devshell.nix @@ -1,59 +1,73 @@ -{inputs, ...}: { - imports = [ - inputs.devshell.flakeModule - ]; +{ + pkgs, + perSystem, + ... +}: let + inherit (pkgs) lib; + inherit (pkgs.stdenv) isLinux isDarwin; +in + perSystem.devshell.mkShell { + env = [ + { + name = "GOROOT"; + value = pkgs.go + "/share/go"; + } + { + name = "LD_LIBRARY_PATH"; + value = "$DEVSHELL_DIR/lib"; + } + ]; - config.perSystem = { - lib, - pkgs, - config, - ... - }: let - inherit (pkgs.stdenv) isLinux isDarwin; - in { - config.devshells.default = { - env = [ - { - name = "GOROOT"; - value = pkgs.go + "/share/go"; - } - { - name = "LD_LIBRARY_PATH"; - value = "$DEVSHELL_DIR/lib"; - } - ]; + packages = lib.mkMerge [ + (with pkgs; [ + # golang + go + goreleaser + golangci-lint + delve + pprof + graphviz - packages = lib.mkMerge [ - (with pkgs; [ - # golang - go - goreleaser - golangci-lint - delve - pprof - graphviz - ]) - # platform dependent CGO dependencies - (lib.mkIf isLinux [ - pkgs.gcc - ]) - (lib.mkIf isDarwin [ - pkgs.darwin.cctools - ]) - # include formatters for development and testing - (import ./formatters.nix pkgs) - ]; + # docs + nodejs + ]) + # platform dependent CGO dependencies + (lib.mkIf isLinux [pkgs.gcc]) + (lib.mkIf isDarwin [pkgs.darwin.cctools]) + # include formatters for development and testing + (import ./packages/treefmt/formatters.nix pkgs) + ]; - commands = [ - { - category = "development"; - package = pkgs.gomod2nix; - } - { - category = "development"; - package = pkgs.enumer; - } - ]; - }; - }; -} + commands = [ + {package = perSystem.gomod2nix.default;} + { + name = "docs:dev"; + help = "serve docs for local development"; + command = "cd $PRJ_ROOT/docs && npm ci && npm run dev"; + } + { + name = "docs:build"; + help = "create a production build of docs"; + command = "cd $PRJ_ROOT/docs && npm ci && npm run build"; + } + { + name = "docs:preview"; + help = "preview a production build of docs"; + command = "cd $PRJ_ROOT/docs && npm ci && npm run preview"; + } + { + help = "generate terminal gifs"; + package = pkgs.writeShellApplication { + name = "vhs"; + runtimeInputs = + [ + perSystem.self.treefmt + pkgs.rsync + pkgs.vhs + ] + ++ (import ./packages/treefmt/formatters.nix pkgs); + text = ''vhs "$@"''; + }; + } + ]; + } diff --git a/nix/docs.nix b/nix/docs.nix deleted file mode 100644 index 9dee9fd..0000000 --- a/nix/docs.nix +++ /dev/null @@ -1,70 +0,0 @@ -_: { - perSystem = { - pkgs, - self', - ... - }: { - packages.docs = pkgs.buildNpmPackage { - pname = "treefmt-docs"; - inherit (self'.packages.default) version; - - src = ../docs; - npmDepsHash = "sha256-vHxJHuhvQJ0P4wS1Hd2BIfPMSptnLhuHGLXCO+P5iFs="; - - # we have to use a custom build phase because vitepress is doing something funky with the ttty - buildPhase = '' - cat | npm run build 2>&1 | cat - ''; - - installPhase = '' - runHook preInstall - cp -rv .vitepress/dist/ $out - runHook postInstall - ''; - }; - - devshells.default = { - packages = [ - pkgs.nodejs - ]; - - commands = let - category = "docs"; - in [ - { - inherit category; - name = "docs:dev"; - help = "serve docs for local development"; - command = "cd $PRJ_ROOT/docs && npm ci && npm run dev"; - } - { - inherit category; - name = "docs:build"; - help = "create a production build of docs"; - command = "cd $PRJ_ROOT/docs && npm ci && npm run build"; - } - { - inherit category; - name = "docs:preview"; - help = "preview a production build of docs"; - command = "cd $PRJ_ROOT/docs && npm ci && npm run preview"; - } - { - inherit category; - help = "generate terminal gifs"; - package = pkgs.writeShellApplication { - name = "vhs"; - runtimeInputs = - [ - self'.packages.treefmt - pkgs.rsync - pkgs.vhs - ] - ++ (import ./formatters.nix pkgs); - text = ''vhs "$@"''; - }; - } - ]; - }; - }; -} diff --git a/nix/formatter.nix b/nix/formatter.nix new file mode 100644 index 0000000..44c3d67 --- /dev/null +++ b/nix/formatter.nix @@ -0,0 +1,51 @@ +{ + pkgs, + inputs, + perSystem, + ... +}: +inputs.treefmt-nix.lib.mkWrapper pkgs { + projectRootFile = "flake.nix"; + + package = perSystem.self.treefmt; + + programs = { + alejandra.enable = true; + deadnix.enable = true; + gofumpt.enable = true; + prettier.enable = true; + statix.enable = true; + }; + + settings = { + global.excludes = [ + "LICENSE" + # let's not mess with the test folder + "test/*" + # unsupported extensions + "*.{gif,png,svg,tape,mts,lock,mod,sum,toml,env,envrc,gitignore}" + ]; + + formatter = { + deadnix = { + priority = 1; + }; + + statix = { + priority = 2; + }; + + alejandra = { + priority = 3; + }; + + prettier = { + options = [ + "--tab-width" + "4" + ]; + includes = ["*.{css,html,js,json,jsx,md,mdx,scss,ts,yaml}"]; + }; + }; + }; +} diff --git a/nix/nixpkgs.nix b/nix/nixpkgs.nix deleted file mode 100644 index fd370ca..0000000 --- a/nix/nixpkgs.nix +++ /dev/null @@ -1,16 +0,0 @@ -{inputs, ...}: { - perSystem = {system, ...}: { - # customise nixpkgs instance - _module.args.pkgs = import inputs.nixpkgs { - inherit system; - overlays = [ - inputs.gomod2nix.overlays.default - ]; - config = { - # for terraform - # todo make this more specific - allowUnfree = true; - }; - }; - }; -} diff --git a/nix/packages.nix b/nix/packages.nix deleted file mode 100644 index 724ca83..0000000 --- a/nix/packages.nix +++ /dev/null @@ -1,77 +0,0 @@ -{ - self, - inputs, - ... -}: { - imports = [ - inputs.flake-parts.flakeModules.easyOverlay - ]; - - perSystem = { - self', - inputs', - lib, - pkgs, - ... - }: { - packages = rec { - treefmt = inputs'.gomod2nix.legacyPackages.buildGoApplication rec { - pname = "treefmt"; - # there's no good way of tying in the version to a git tag or branch - # so for simplicity's sake we set the version as the commit revision hash - # we remove the `-dirty` suffix to avoid a lot of unnecessary rebuilds in local dev - version = lib.removeSuffix "-dirty" (self.shortRev or self.dirtyShortRev); - - # ensure we are using the same version of go to build with - inherit (pkgs) go; - - src = let - filter = inputs.nix-filter.lib; - in - filter { - root = ../.; - exclude = [ - "nix/" - "docs/" - ".github/" - "README.md" - "default.nix" - "shell.nix" - ".envrc" - ]; - }; - - modules = ../gomod2nix.toml; - - CGO_ENABLED = 1; - - ldflags = [ - "-s" - "-w" - "-X git.numtide.com/numtide/treefmt/build.Name=${pname}" - "-X git.numtide.com/numtide/treefmt/build.Version=v${version}" - ]; - - nativeBuildInputs = - # we need some formatters available for the tests - import ./formatters.nix pkgs; - - preCheck = '' - XDG_CACHE_HOME=$(mktemp -d) - export XDG_CACHE_HOME - ''; - - meta = with lib; { - description = "treefmt: one CLI to format your repo"; - homepage = "https://git.numtide.com/numtide/treefmt"; - license = licenses.mit; - mainProgram = "treefmt"; - }; - }; - - default = treefmt; - }; - - overlayAttrs = self'.packages; - }; -} diff --git a/nix/packages/default.nix b/nix/packages/default.nix new file mode 100644 index 0000000..ea9a9fe --- /dev/null +++ b/nix/packages/default.nix @@ -0,0 +1 @@ +{perSystem, ...}: perSystem.self.treefmt diff --git a/nix/packages/treefmt-docs.nix b/nix/packages/treefmt-docs.nix new file mode 100644 index 0000000..93fb4fe --- /dev/null +++ b/nix/packages/treefmt-docs.nix @@ -0,0 +1,25 @@ +{ + pname, + pkgs, + flake, + perSystem, + ... +}: +pkgs.buildNpmPackage { + inherit pname; + inherit (perSystem.self.treefmt) version; + + src = "${flake}/docs"; + npmDepsHash = "sha256-vHxJHuhvQJ0P4wS1Hd2BIfPMSptnLhuHGLXCO+P5iFs="; + + # we have to use a custom build phase because vitepress is doing something funky with the ttty + buildPhase = '' + cat | npm run build 2>&1 | cat + ''; + + installPhase = '' + runHook preInstall + cp -rv .vitepress/dist/ $out + runHook postInstall + ''; +} diff --git a/nix/packages/treefmt/default.nix b/nix/packages/treefmt/default.nix new file mode 100644 index 0000000..ea23d1f --- /dev/null +++ b/nix/packages/treefmt/default.nix @@ -0,0 +1,64 @@ +{ + pname, + pkgs, + flake, + inputs, + perSystem, + ... +}: let + inherit (pkgs) lib; +in + perSystem.gomod2nix.buildGoApplication rec { + inherit pname; + # there's no good way of tying in the version to a git tag or branch + # so for simplicity's sake we set the version as the commit revision hash + # we remove the `-dirty` suffix to avoid a lot of unnecessary rebuilds in local dev + version = lib.removeSuffix "-dirty" (flake.shortRev or flake.dirtyShortRev); + + # ensure we are using the same version of go to build with + inherit (pkgs) go; + + src = let + filter = inputs.nix-filter.lib; + in + filter { + root = ../../../.; + exclude = [ + "nix/" + "docs/" + ".github/" + "README.md" + "default.nix" + "shell.nix" + ".env" + ".envrc" + ]; + }; + + modules = ./gomod2nix.toml; + + CGO_ENABLED = 1; + + ldflags = [ + "-s" + "-w" + "-X git.numtide.com/numtide/treefmt/build.Name=${pname}" + "-X git.numtide.com/numtide/treefmt/build.Version=v${version}" + ]; + + nativeBuildInputs = + # we need some formatters available for the tests + import ./formatters.nix pkgs; + + preCheck = '' + XDG_CACHE_HOME=$(mktemp -d) + export XDG_CACHE_HOME + ''; + + meta = with lib; { + description = "treefmt: one CLI to format your repo"; + homepage = "https://git.numtide.com/numtide/treefmt"; + license = licenses.mit; + mainProgram = "treefmt"; + }; + } diff --git a/nix/formatters.nix b/nix/packages/treefmt/formatters.nix similarity index 100% rename from nix/formatters.nix rename to nix/packages/treefmt/formatters.nix diff --git a/gomod2nix.toml b/nix/packages/treefmt/gomod2nix.toml similarity index 100% rename from gomod2nix.toml rename to nix/packages/treefmt/gomod2nix.toml diff --git a/nix/treefmt.nix b/nix/treefmt.nix deleted file mode 100644 index 4a97ec0..0000000 --- a/nix/treefmt.nix +++ /dev/null @@ -1,66 +0,0 @@ -{inputs, ...}: { - imports = [ - inputs.treefmt-nix.flakeModule - ]; - perSystem = { - config, - self', - ... - }: { - treefmt.config = { - flakeCheck = true; - flakeFormatter = true; - projectRootFile = "flake.nix"; - - package = self'.packages.default; - - programs = { - alejandra.enable = true; - deadnix.enable = true; - gofumpt.enable = true; - prettier.enable = true; - statix.enable = true; - }; - - settings = { - global.excludes = [ - "LICENSE" - # let's not mess with the test folder - "test/*" - # unsupported extensions - "*.{gif,png,svg,tape,mts,lock,mod,sum,toml,env,envrc,gitignore}" - ]; - - formatter = { - deadnix = { - priority = 1; - }; - - statix = { - priority = 2; - }; - - alejandra = { - priority = 3; - }; - - prettier = { - options = ["--tab-width" "4"]; - includes = ["*.{css,html,js,json,jsx,md,mdx,scss,ts,yaml}"]; - }; - }; - }; - }; - - devshells.default = { - commands = [ - { - category = "formatting"; - name = "fmt"; - help = "format the repo"; - command = "nix fmt"; - } - ]; - }; - }; -} diff --git a/shell.nix b/shell.nix index a322436..2f6a900 100644 --- a/shell.nix +++ b/shell.nix @@ -2,7 +2,13 @@ {system ? builtins.currentSystem}: let lock = builtins.fromJSON (builtins.readFile ./flake.lock); - inherit (lock.nodes.flake-compat.locked) owner repo rev narHash; + inherit + (lock.nodes.flake-compat.locked) + owner + repo + rev + narHash + ; flake-compat = fetchTarball { url = "https://github.com/${owner}/${repo}/archive/${rev}.tar.gz";