From 28f8e3b7b276105430945d64cbe6f82443167785 Mon Sep 17 00:00:00 2001 From: grabowski Date: Tue, 7 Apr 2026 10:04:14 +0700 Subject: [PATCH] Add NixOS flake with dev shell, package, and systemd module Includes nix develop shell (node, pg, vips), buildNpmPackage derivation, and a NixOS module for deploying as a systemd service with hardening, dedicated user, and configurable secrets via environmentFile. Co-Authored-By: Claude Opus 4.6 (1M context) --- .envrc | 1 + .gitignore | 2 + README.md | 71 +++++++++++++++++++ flake.nix | 202 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 276 insertions(+) create mode 100644 .envrc create mode 100644 flake.nix diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore index 96ee1ad..7364942 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ build static/uploads/** !static/uploads/.gitkeep *.db +result +.direnv diff --git a/README.md b/README.md index 5b262ff..87a8015 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,77 @@ npm run build npm run preview ``` +## NixOS + +This project includes a Nix flake with a dev shell, package, and NixOS module. + +### Development shell + +```bash +nix develop +npm install +npm run dev +``` + +Or with direnv (`.envrc` is included): + +```bash +direnv allow +npm install +npm run dev +``` + +### Build with Nix + +```bash +nix build +``` + +### NixOS module (deploy as a service) + +Add this flake to your NixOS configuration inputs, then: + +```nix +# flake.nix +{ + inputs.buildfor-life-repair.url = "git+https://git.b4l.co.th/B4L/buildfor_life_repair.git"; +} +``` + +```nix +# configuration.nix +{ inputs, ... }: +{ + imports = [ inputs.buildfor-life-repair.nixosModules.default ]; + + services.buildfor-life-repair = { + enable = true; + port = 3000; + databaseUrl = "postgresql://bflr:password@localhost:5432/buildfor_life_repair"; + baseUrl = "https://repair.example.com"; + openFirewall = true; + }; +} +``` + +Or use an environment file for secrets: + +```nix +services.buildfor-life-repair = { + enable = true; + port = 3000; + environmentFile = "/run/secrets/bflr-env"; # contains DATABASE_URL=... + baseUrl = "https://repair.example.com"; +}; +``` + +The module creates a systemd service with: +- Dedicated `bflr` user/group +- Upload directory at `/var/lib/buildfor-life-repair/uploads` +- Systemd hardening (NoNewPrivileges, ProtectSystem, PrivateTmp) +- Auto-restart on failure +- Optional firewall rule + ## Tech stack - **Frontend:** SvelteKit 5, Svelte 5 (runes), Tailwind CSS v4 diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..82d2da4 --- /dev/null +++ b/flake.nix @@ -0,0 +1,202 @@ +{ + description = "buildfor_life_repair - Vintage equipment inventory & repair tracker"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, flake-utils }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { inherit system; }; + nodejs = pkgs.nodejs_20; + in + { + # Development shell: `nix develop` + devShells.default = pkgs.mkShell { + buildInputs = [ + nodejs + pkgs.postgresql_16 + pkgs.pkg-config + pkgs.vips # required by sharp + ]; + + shellHook = '' + echo "buildfor_life_repair dev shell" + echo " node: $(node --version)" + echo " npm: $(npm --version)" + echo "" + echo "Run 'npm install' then 'npm run dev' to start." + ''; + }; + + # Production package: `nix build` + packages.default = pkgs.buildNpmPackage rec { + pname = "buildfor_life_repair"; + version = "0.1.0"; + + src = ./.; + + npmDepsHash = ""; # Run `nix build` once to get the correct hash + + nativeBuildInputs = [ + pkgs.pkg-config + pkgs.python3 # needed by some native modules + ]; + + buildInputs = [ + pkgs.vips # sharp + pkgs.glib + pkgs.expat + ]; + + npmBuildScript = "build"; + + installPhase = '' + runHook preInstall + + mkdir -p $out/lib/buildfor_life_repair + cp -r build/* $out/lib/buildfor_life_repair/ + cp -r node_modules $out/lib/buildfor_life_repair/ + cp package.json $out/lib/buildfor_life_repair/ + + mkdir -p $out/bin + cat > $out/bin/buildfor_life_repair <