From 25370f21786fc70b0c31d7a16347bac75d2a37b4 Mon Sep 17 00:00:00 2001 From: Sigmanificient Date: Wed, 4 Dec 2024 04:15:18 +0100 Subject: [PATCH] Add nix builder, extract pre-commit hooks --- Makefile | 13 ++++++------- flake.nix | 26 +++++++------------------- mk-recipes.mk | 4 ++-- nix/crash.nix | 27 +++++++++++++++++++++++++++ nix/hooks.nix | 20 ++++++++++++++++++++ 5 files changed, 62 insertions(+), 28 deletions(-) create mode 100644 nix/crash.nix create mode 100644 nix/hooks.nix diff --git a/Makefile b/Makefile index 37f1973..13c6d47 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,6 @@ CC := gcc CFLAGS += -std=c99 -pedantic CFLAGS += -iquote $/ -CFLAGS += -O2 ifneq ($(EXPLICIT_FLAGS),1) CFLAGS += @$/base_warnings @@ -35,15 +34,15 @@ include $/mk-recipes.mk release-flags := -O3 -DNDEBUG #? crash: build the release binary, crash -$(eval $(call mk-binary, crash, SRC, $(release-flags))) +$(eval $(call mk-recipe-binary, crash, SRC, $(release-flags))) #? debug: build with debug logs an eponym binary -debug-flags := -fanalyzer -DDEBUG=1 -g3 -$(eval $(call mk-binary, debug, SRC, $(debug-flags))) +debug-flags := -O2 -fanalyzer -DDEBUG=1 -g3 +$(eval $(call mk-recipe-binary, debug, SRC, $(debug-flags))) #? check: build with all warnings and sanitizers an eponym binary check-flags := $(debug-flags) -fsanitize=address,leak,undefined -Wpadded -$(eval $(call mk-binary, check, SRC, $(check-flags))) +$(eval $(call mk-recipe-binary, check, SRC, $(check-flags))) .PHONY: all all: $(out-crash) @@ -76,8 +75,8 @@ help: #? help: show this help message #? install: package within the provided dir .PHONY: install -install: $(OUT) - install -Dm755 -t $(PREFIX)/bin $(OUT) +install: $(out-crash) + install -Dm755 -t $(PREFIX)/bin $(out-crash) ifneq ($(shell command -v tput),) ifneq ($(shell tput colors),0) diff --git a/flake.nix b/flake.nix index f50c53e..bd7492f 100644 --- a/flake.nix +++ b/flake.nix @@ -22,25 +22,7 @@ formatter = forAllSystems (pkgs: pkgs.alejandra); checks = forAllSystems (pkgs: let - pyrun = name: pkgs.python310.interpreter + " " + name; - hooks = { - alejandra.enable = true; - black.enable = true; - trim-trailing-whitespace.enable = true; - - commit-name = { - enable = true; - name = "commit name"; - stages = ["commit-msg"]; - entry = pyrun ./scripts/check_commit_msg.py; - }; - - check-include-order = { - enable = true; - name = "commit name"; - entry = pyrun ./scripts/check_include_order.py; - }; - }; + hooks = import ./nix/hooks.nix {inherit pkgs;}; in { pre-commit-check = pre-commit-hooks.lib.${pkgs.system}.run { inherit hooks; @@ -60,5 +42,11 @@ ]; }; }); + + packages = forAllSystems (pkgs: { + default = self.packages.${pkgs.system}.crash; + crash = pkgs.callPackage ./nix/crash.nix {}; + bundle = pkgs.callPackage ./nix/crash.nix {withDevBinaries = true;}; + }); }; } diff --git a/mk-recipes.mk b/mk-recipes.mk index 232dadc..1d8daf6 100644 --- a/mk-recipes.mk +++ b/mk-recipes.mk @@ -17,14 +17,14 @@ $$(BUILD_DIR)/$(strip $1)/%.o: %.c $$Q $$(CC) \ $$(CFLAGS) $$(CFLAGS_@$$(notdir $$(@:.o=))) $3 \ -o $$@ -c $$< - @ $$(LOG_TIME) "CC $$(C_PURPLE) $$(notdir $$@) $$(C_RESET)" + @ $$(log) "CC $$(C_PURPLE) $$(notdir $$@) $$(C_RESET)" $$(out-$(strip $1)): $$(obj-$(strip $1)) @ mkdir -p $$(dir $$@) $$Q $$(CC) -o $$@ $$(obj-$(strip $1)) \ $$(CFLAGS) $$(CFLAGS_@$$(notdir $$(@:.o=))) $3 \ $$(LDLIBS) $$(LDFLAGS) - @ $$(LOG_TIME) "LD $$(C_GREEN) $$@ $$(C_RESET)" + @ $$(log) "LD $$(C_GREEN) $$@ $$(C_RESET)" _clean += $$(obj-$(strip $1)) _fclean += $$(out-$(strip $1)) diff --git a/nix/crash.nix b/nix/crash.nix new file mode 100644 index 0000000..facb8ab --- /dev/null +++ b/nix/crash.nix @@ -0,0 +1,27 @@ +{ + stdenv, + lib, + withDevBinaries ? false, +}: +stdenv.mkDerivation { + name = "crash"; + version = "0.1"; + + src = ./..; + makeFlags = ["PREFIX=${placeholder "out"}"]; + + buildFlags = ["crash"] ++ (lib.optional withDevBinaries ["debug" "check"]); + + postInstall = lib.optional withDevBinaries '' + install -Dm755 -t $out/bin debug + install -Dm755 -t $out/bin check + ''; + + meta = { + description = "Crash resilient auspicious shell."; + maintainers = with lib.maintainers; [savalet sigmanificient]; + license = lib.licenses.mit; + platforms = lib.platforms.linux; + mainProgram = "crash"; + }; +} diff --git a/nix/hooks.nix b/nix/hooks.nix new file mode 100644 index 0000000..ef57a82 --- /dev/null +++ b/nix/hooks.nix @@ -0,0 +1,20 @@ +{pkgs}: let + pyrun = name: pkgs.python310.interpreter + " " + name; +in { + alejandra.enable = true; + black.enable = true; + trim-trailing-whitespace.enable = true; + + commit-name = { + enable = true; + name = "check commit name"; + stages = ["commit-msg"]; + entry = pyrun ../scripts/check_commit_msg.py; + }; + + check-include-order = { + enable = true; + name = "check include order"; + entry = pyrun ../scripts/check_include_order.py; + }; +}