From fe9ac77d9259b7f963eb58a3556c57fdb8a778b4 Mon Sep 17 00:00:00 2001 From: Stefan Date: Thu, 9 Jan 2025 22:03:37 +0000 Subject: [PATCH] Merge Nix files together and add htmlbeautifier --- bin/build | 1 + bin/serve | 0 default.nix | 67 +++++++++----------------------------- flake.lock | 3 +- flake.nix | 74 ++++++++++++++++++++++++++++++----------- shell.nix | 94 ----------------------------------------------------- 6 files changed, 73 insertions(+), 166 deletions(-) mode change 100644 => 100755 bin/build mode change 100644 => 100755 bin/serve delete mode 100644 shell.nix diff --git a/bin/build b/bin/build old mode 100644 new mode 100755 index 7c77161..ae907df --- a/bin/build +++ b/bin/build @@ -2,3 +2,4 @@ sass --update style:_site/style --style compressed yarn eleventy +find _site -name "*.html" -exec htmlbeautifier {} \; diff --git a/bin/serve b/bin/serve old mode 100644 new mode 100755 diff --git a/default.nix b/default.nix index 2a9bb88..88f86cf 100644 --- a/default.nix +++ b/default.nix @@ -1,52 +1,15 @@ -{ - pkgs ? import { }, -}: - -let - # Input source files - src = ./.; - nodeDeps = import ./node-deps.nix { inherit pkgs; }; - inherit (nodeDeps) packageJSON nodeModules; -in -pkgs.stdenv.mkDerivation { - name = "veganprestwich-co-uk"; - - src = builtins.filterSource ( - path: type: - !(builtins.elem (baseNameOf path) [ - "_site" - "node_modules" - ".git" - ]) - ) src; - - nativeBuildInputs = with pkgs; [ - cacert - lightningcss - sass - yarn - ]; - - configurePhase = '' - export HOME=$TMPDIR - mkdir -p _site/style - - cp -r ${nodeModules}/node_modules . - chmod -R +w node_modules - cp ${packageJSON} package.json - ''; - - buildPhase = '' - echo 'Building CSS' - sass --update style:_site/style --style compressed - - echo 'Building site' - yarn --offline eleventy - ''; - - installPhase = '' - mkdir -p $out - cp -r _site/* $out/ - rm -rf node_modules _site package.json - ''; -} +(import + ( + let + lock = builtins.fromJSON (builtins.readFile ./flake.lock); + flake-compat = fetchTarball { + url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; + sha256 = lock.nodes.flake-compat.locked.narHash; + }; + in + flake-compat + ) + { + src = ./.; + } +).defaultNix.packages.${builtins.currentSystem}.site diff --git a/flake.lock b/flake.lock index e4379b6..e549fe8 100644 --- a/flake.lock +++ b/flake.lock @@ -5,7 +5,6 @@ "systems": "systems" }, "locked": { - "lastModified": 1731533236, "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", "owner": "numtide", "repo": "flake-utils", @@ -13,8 +12,10 @@ "type": "github" }, "original": { + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", "owner": "numtide", "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", "type": "github" } }, diff --git a/flake.nix b/flake.nix index 994c772..7f75bb0 100644 --- a/flake.nix +++ b/flake.nix @@ -1,7 +1,7 @@ { - description = "renegade-solar.co.uk"; + description = "vegan-prestwich"; inputs = { - flake-utils.url = "github:numtide/flake-utils"; + flake-utils.url = "github:numtide/flake-utils/11707dc2f618dd54ca8739b309ec4fc024de578b?narHash=sha256-l0KFg5HjrsfsO/JpG%2Br7fRrqm12kzFHyUHqHCVpMMbI%3D"; }; outputs = { @@ -12,30 +12,69 @@ flake-utils.lib.eachDefaultSystem ( system: let + pkgs = import nixpkgs { inherit system; }; nodeDeps = import ./node-deps.nix { inherit pkgs; }; inherit (nodeDeps) packageJSON nodeModules; + projectName = builtins.baseNameOf ./.; - pkgs = import nixpkgs { - inherit system; - }; - - # Common build inputs commonBuildInputs = with pkgs; [ djlint + rubyPackages_3_4.htmlbeautifier sass vscode-langservers-extracted yarn yarn2nix ]; - # Helper function to create scripts + setupNodeModules = '' + rm -rf node_modules package.json + ln -sf ${packageJSON} package.json + ln -sf ${nodeModules}/node_modules . + ''; + + siteDrv = pkgs.stdenv.mkDerivation { + name = projectName; + + src = builtins.filterSource ( + path: type: + !(builtins.elem (baseNameOf path) [ + "_site" + "node_modules" + ".git" + ]) + ) ./.; + + nativeBuildInputs = + with pkgs; + [ + cacert + lightningcss + ] + ++ commonBuildInputs; + + configurePhase = '' + export HOME=$TMPDIR + mkdir -p _site/style + ${setupNodeModules} + ''; + + buildPhase = '' + ${./bin/build} + ''; + + installPhase = '' + mkdir -p $out + cp -r _site/* $out/ + rm -rf node_modules _site package.json + ''; + }; + mkScript = name: (pkgs.writeScriptBin name (builtins.readFile ./bin/${name})).overrideAttrs (old: { buildCommand = "${old.buildCommand}\n patchShebangs $out"; }); - # Helper function to create packages mkPackage = name: pkgs.symlinkJoin { @@ -45,13 +84,11 @@ postBuild = "wrapProgram $out/bin/${name} --prefix PATH : $out/bin"; }; - # Script names scripts = [ "build" "serve" ]; - # Generate all packages scriptPackages = builtins.listToAttrs ( map (name: { inherit name; @@ -60,19 +97,18 @@ ); in - rec { - defaultPackage = packages.serve; - packages = scriptPackages; + { + defaultPackage = siteDrv; + packages = scriptPackages // { + site = siteDrv; + }; devShells = rec { default = dev; dev = pkgs.mkShell { - buildInputs = commonBuildInputs ++ (builtins.attrValues packages); + buildInputs = commonBuildInputs ++ (builtins.attrValues scriptPackages); shellHook = '' - rm -rf node_modules - rm -rf package.json - ln -sf ${packageJSON} package.json - ln -sf ${nodeModules}/node_modules . + ${setupNodeModules} echo "Development environment ready!" echo "Run 'serve' to start development server" echo "Run 'build' to build the site in the _site directory" diff --git a/shell.nix b/shell.nix deleted file mode 100644 index 288b44d..0000000 --- a/shell.nix +++ /dev/null @@ -1,94 +0,0 @@ -{ - pkgs ? import { }, -}: - -let - nodeDeps = import ./node-deps.nix { inherit pkgs; }; - inherit (nodeDeps) packageJSON nodeModules; -in -pkgs.mkShell { - buildInputs = with pkgs; [ - yarn - sass - lightningcss - ]; - - shellHook = '' - rm -rf node_modules - rm -rf package.json - - git pull - - ln -sf ${packageJSON} package.json - ln -sf ${nodeModules}/node_modules . - - serve() { - mkdir -p _site/style - - sass --watch style/style.scss:_site/style/style.css & - SASS_PID=$! - - yarn eleventy --serve & - ELEVENTY_PID=$! - - cleanup_serve() { - echo "Cleaning up serve processes..." - kill $SASS_PID 2>/dev/null - kill $ELEVENTY_PID 2>/dev/null - wait $SASS_PID 2>/dev/null - wait $ELEVENTY_PID 2>/dev/null - } - - trap cleanup_serve EXIT INT TERM - - wait -n - - cleanup_serve - - trap - EXIT INT TERM - } - - upgrade_deps() { - local mode=''${1:-"minor"} - - case $mode in - "major") - echo "Upgrading to latest major versions..." - yarn upgrade --latest 2>/dev/null - ;; - "minor") - echo "Upgrading to latest minor versions..." - yarn upgrade 2>/dev/null - ;; - "patch") - echo "Upgrading patch versions only..." - yarn upgrade --pattern "*" --target patch 2>/dev/null - ;; - *) - echo "Unknown mode: $mode" - echo "Usage: upgrade_deps [major|minor|patch]" - return 1 - ;; - esac - - yarn install 2>/dev/null - - echo "Done! Don't forget to commit the updated yarn.lock file." - exit 0 - } - - export -f upgrade_deps - export -f serve - - cleanup() { - echo "Cleaning up..." - rm -rf node_modules _site package.json - } - - trap cleanup EXIT - - echo "Development environment ready!" - echo "Run 'serve' to start development server" - echo "Run 'upgrade_deps [major|minor|patch]' to upgrade your dependencies" - ''; -}