Add pre-commit

main
mat ess 2022-12-21 22:08:18 -05:00
parent a512e2a078
commit 130efc7383
8 changed files with 136 additions and 28 deletions

3
.gitignore vendored
View File

@ -10,3 +10,6 @@ static/style/fonts.css
# Local Netlify folder # Local Netlify folder
.netlify .netlify
# local pre-commit config
.pre-commit-config.yaml

2
_typos.toml Normal file
View File

@ -0,0 +1,2 @@
[default.extend-words]
referer = "referer"

View File

@ -126,7 +126,7 @@ If software builds are functions, then Nix flakes are pure functions. Indeed, th
builder.url = "..."; builder.url = "...";
source.url = "..."; source.url = "...";
}; };
ouputs = { self, builder, source }: { outputs = { self, builder, source }: {
software = builder.build source; software = builder.build source;
}; };
} }

View File

@ -16,18 +16,32 @@
"type": "github" "type": "github"
} }
}, },
"flake-compat": {
"flake": false,
"locked": {
"lastModified": 1668681692,
"narHash": "sha256-Ht91NGdewz8IQLtWZ9LCeNXMSXHUss+9COoqu6JLmXU=",
"owner": "edolstra",
"repo": "flake-compat",
"rev": "009399224d5e398d03b22badca40a37ac85412a1",
"type": "github"
},
"original": {
"owner": "edolstra",
"repo": "flake-compat",
"type": "github"
}
},
"flake-parts": { "flake-parts": {
"inputs": { "inputs": {
"nixpkgs": [ "nixpkgs-lib": "nixpkgs-lib"
"nixpkgs"
]
}, },
"locked": { "locked": {
"lastModified": 1664385320, "lastModified": 1668450977,
"narHash": "sha256-uSCHb9RUqHRjTpI8OzyaUuHckFnnxwZqlXEuo+yUpYQ=", "narHash": "sha256-cfLhMhnvXn6x1vPm+Jow3RiFAUSCw/l1utktCw5rVA4=",
"owner": "hercules-ci", "owner": "hercules-ci",
"repo": "flake-parts", "repo": "flake-parts",
"rev": "45298bd207b26e1bcd030a0bb075aa2da29f8e30", "rev": "d591857e9d7dd9ddbfba0ea02b43b927c3c0f1fa",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -36,6 +50,21 @@
"type": "github" "type": "github"
} }
}, },
"flake-utils": {
"locked": {
"lastModified": 1667395993,
"narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"gitignore": { "gitignore": {
"inputs": { "inputs": {
"nixpkgs": [ "nixpkgs": [
@ -58,11 +87,11 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1665259268, "lastModified": 1670064435,
"narHash": "sha256-ONFhHBLv5nZKhwV/F2GOH16197PbvpyWhoO0AOyktkU=", "narHash": "sha256-+ELoY30UN+Pl3Yn7RWRPabykwebsVK/kYE9JsIsUMxQ=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "c5924154f000e6306030300592f4282949b2db6c", "rev": "61a8a98e6d557e6dd7ed0cdb54c3a3e3bbc5e25c",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -72,12 +101,73 @@
"type": "github" "type": "github"
} }
}, },
"nixpkgs-lib": {
"locked": {
"dir": "lib",
"lastModified": 1665349835,
"narHash": "sha256-UK4urM3iN80UXQ7EaOappDzcisYIuEURFRoGQ/yPkug=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "34c5293a71ffdb2fe054eb5288adc1882c1eb0b1",
"type": "github"
},
"original": {
"dir": "lib",
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs-stable": {
"locked": {
"lastModified": 1671271954,
"narHash": "sha256-cSvu+bnvN08sOlTBWbBrKaBHQZq8mvk8bgpt0ZJ2Snc=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "d513b448cc2a6da2c8803e3c197c9fc7e67b19e3",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-22.05",
"repo": "nixpkgs",
"type": "github"
}
},
"pre-commit": {
"inputs": {
"flake-compat": "flake-compat",
"flake-utils": "flake-utils",
"gitignore": [
"gitignore"
],
"nixpkgs": [
"nixpkgs"
],
"nixpkgs-stable": "nixpkgs-stable"
},
"locked": {
"lastModified": 1671452357,
"narHash": "sha256-HqzXiQEegpRQ4VEl9pEPgHSIxhJrNJ27HfN1wOc7w2E=",
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"rev": "200790e9c77064c53eaf95805b013d96615ecc27",
"type": "github"
},
"original": {
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"type": "github"
}
},
"root": { "root": {
"inputs": { "inputs": {
"caddyfile-syntax": "caddyfile-syntax", "caddyfile-syntax": "caddyfile-syntax",
"flake-parts": "flake-parts", "flake-parts": "flake-parts",
"gitignore": "gitignore", "gitignore": "gitignore",
"nixpkgs": "nixpkgs" "nixpkgs": "nixpkgs",
"pre-commit": "pre-commit"
} }
} }
}, },

View File

@ -4,19 +4,21 @@
inputs = { inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-parts.url = "github:hercules-ci/flake-parts"; flake-parts.url = "github:hercules-ci/flake-parts";
flake-parts.inputs.nixpkgs.follows = "nixpkgs";
gitignore.url = "github:hercules-ci/gitignore.nix"; gitignore.url = "github:hercules-ci/gitignore.nix";
gitignore.inputs.nixpkgs.follows = "nixpkgs"; gitignore.inputs.nixpkgs.follows = "nixpkgs";
pre-commit.url = "github:cachix/pre-commit-hooks.nix";
pre-commit.inputs.nixpkgs.follows = "nixpkgs";
pre-commit.inputs.gitignore.follows = "gitignore";
caddyfile-syntax.url = "github:caddyserver/sublimetext"; caddyfile-syntax.url = "github:caddyserver/sublimetext";
caddyfile-syntax.flake = false; caddyfile-syntax.flake = false;
}; };
outputs = { self, flake-parts, gitignore, ... }@inputs: outputs = { self, flake-parts, gitignore, pre-commit, ... }@inputs:
flake-parts.lib.mkFlake { inherit self; } { flake-parts.lib.mkFlake { inherit self; } {
imports = [ ]; imports = [ pre-commit.flakeModule ];
systems = [ "x86_64-linux" "aarch64-darwin" ]; systems = [ "x86_64-linux" "aarch64-darwin" ];
perSystem = { config, self', inputs', pkgs, system, ... }: perSystem = { config, pkgs, ... }:
let let
inherit (gitignore.lib) gitignoreSource; inherit (gitignore.lib) gitignoreSource;
inherit (pkgs.callPackage ./nix { }) fonts optimize-images update-date; inherit (pkgs.callPackage ./nix { }) fonts optimize-images update-date;
@ -24,25 +26,23 @@
caddyfile-syntax = "${inputs.caddyfile-syntax}/Caddyfile.sublime-syntax"; caddyfile-syntax = "${inputs.caddyfile-syntax}/Caddyfile.sublime-syntax";
buildSite = { prod }: buildSite = { prod }:
let let
inherit (pkgs.lib) optionalString; ifStaging = pkgs.lib.optionalString (!prod);
ifStaging = optionalString (!prod);
rev = if (self ? rev) then self.rev else "dirty";
in in
'' ''
optimize-images optimize-images
zola build --drafts ${ifStaging "--base-url https://staging--mat-services.netlify.app"} zola build --drafts ${ifStaging "--base-url https://staging--mat-services.netlify.app"}
# zola's ignored_content setting doesn't work in static/ # zola's ignored_content setting doesn't work in static/
rm -rf public/image/_favicon.svg rm -rf public/image/_favicon.svg
convert public/image/favicon.svg -resize 256x256 public/favicon.ico
''; '';
in in
{ {
packages.default = with pkgs; stdenv.mkDerivation { packages.default = with pkgs; stdenv.mkDerivation {
pname = "personal-site"; pname = "personal-site";
version = "2022-11-25"; version = "2022-12-21";
src = gitignoreSource ./.; src = gitignoreSource ./.;
nativeBuildInputs = [ imagemagick optimize-images update-date zola ]; nativeBuildInputs = [ optimize-images update-date zola ];
configurePhase = copyFonts + '' configurePhase = ''
${copyFonts}
mkdir -p extra/syntax mkdir -p extra/syntax
cp ${caddyfile-syntax} extra/syntax cp ${caddyfile-syntax} extra/syntax
''; '';
@ -54,12 +54,25 @@
packages.staging-site = config.packages.default.overrideAttrs (_: { packages.staging-site = config.packages.default.overrideAttrs (_: {
buildPhase = buildSite { prod = false; }; buildPhase = buildSite { prod = false; };
}); });
pre-commit.settings.hooks = {
# nix hooks
deadnix.enable = true;
nix-linter.enable = true;
nixpkgs-fmt.enable = true;
statix.enable = true;
# general hooks
typos.enable = true;
typos.excludes = [ "webp" "png" "svg" "ico" ];
};
devShells.default = with pkgs; mkShell { devShells.default = with pkgs; mkShell {
packages = [ optimize-images update-date zola ]; packages = [ jsonnet netlify-cli optimize-images update-date zola ];
shellHook = linkFonts + '' shellHook = ''
${config.pre-commit.installationScript}
${linkFonts}
mkdir -p extra/syntax mkdir -p extra/syntax
ln -snf ${caddyfile-syntax} extra/syntax ln -snf ${caddyfile-syntax} extra/syntax
''; '';
inputsFrom = builtins.attrValues self.checks;
}; };
}; };
}; };

View File

@ -2,7 +2,6 @@
, lib , lib
, stdenv , stdenv
, woff2 , woff2
, writeShellScriptBin
, writeText , writeText
}: }:
let let
@ -45,7 +44,7 @@ rec {
name = "font-${pathName name}"; name = "font-${pathName name}";
src = fetchFromGitHub { src = fetchFromGitHub {
inherit owner repo rev sha256; inherit owner repo rev sha256;
sparseCheckout = path; sparseCheckout = [ path ];
}; };
nativeBuildInputs = [ woff2 ]; nativeBuildInputs = [ woff2 ];
buildPhase = '' buildPhase = ''
@ -61,7 +60,7 @@ rec {
font = fontDerivation fontSrc; font = fontDerivation fontSrc;
}; };
fonts = map fetchFont fontSrcs; fonts = map fetchFont fontSrcs;
mkFontCss = { font, name, fileName, cssVar }: mkFontCss = { name, fileName, cssVar, ... }:
let path = pathName name; in '' let path = pathName name; in ''
@font-face { @font-face {
font-family: '${name}'; font-family: '${name}';

View File

@ -1,4 +1,4 @@
{ bc, libwebp, scour, writeShellScriptBin }: { bc, imagemagick, libwebp, scour, writeShellScriptBin }:
writeShellScriptBin "optimize-images" '' writeShellScriptBin "optimize-images" ''
shopt -s globstar nullglob shopt -s globstar nullglob
@ -14,4 +14,5 @@ writeShellScriptBin "optimize-images" ''
pct=$(${bc}/bin/bc <<< "scale=1; $newsize * 100 / $oldsize") pct=$(${bc}/bin/bc <<< "scale=1; $newsize * 100 / $oldsize")
echo "size went from "$(($oldsize / 1024))"KB to "$(($newsize / 1024))"KB ("$pct"% as large as original)" echo "size went from "$(($oldsize / 1024))"KB to "$(($newsize / 1024))"KB ("$pct"% as large as original)"
done done
${imagemagick}/bin/convert static/image/favicon.svg -resize 256x256 static/favicon.ico
'' ''

BIN
static/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB