feat: use numtide/blueprint instead of flake.parts

Signed-off-by: Brian McGee <brian@bmcgee.ie>
This commit is contained in:
Brian McGee 2024-06-28 20:11:33 +01:00
parent 30d30b54f0
commit 986f020c3d
No known key found for this signature in database
GPG Key ID: D49016E76AD1E8C0
19 changed files with 295 additions and 363 deletions

4
.envrc
View File

@ -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

View File

@ -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

View File

@ -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";

View File

@ -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": [

View File

@ -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;
};
};
}

View File

@ -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;
};
}

View File

@ -1,10 +0,0 @@
{...}: {
imports = [
./checks.nix
./devshell.nix
./docs.nix
./nixpkgs.nix
./packages.nix
./treefmt.nix
];
}

View File

@ -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 "$@"'';
};
}
];
}

View File

@ -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 "$@"'';
};
}
];
};
};
}

51
nix/formatter.nix Normal file
View File

@ -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}"];
};
};
};
}

View File

@ -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;
};
};
};
}

View File

@ -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;
};
}

1
nix/packages/default.nix Normal file
View File

@ -0,0 +1 @@
{perSystem, ...}: perSystem.self.treefmt

View File

@ -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
'';
}

View File

@ -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";
};
}

View File

@ -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";
}
];
};
};
}

View File

@ -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";