Add serve command to shell.nix

This commit is contained in:
Stefan 2024-11-27 09:17:45 +00:00
parent a128354423
commit 1bec9aff25
2 changed files with 36 additions and 28 deletions

View file

@ -19,57 +19,39 @@ pkgs.stdenv.mkDerivation {
src; src;
nativeBuildInputs = with pkgs; [ nativeBuildInputs = with pkgs; [
sass
lightningcss
nodejs_22
yarn
cacert cacert
curl
glib
html-minifier html-minifier
lightningcss
sass
yarn
]; ];
SSL_CERT_FILE = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt";
NIX_SSL_CERT_FILE = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt";
GIT_SSL_CAINFO = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt";
configurePhase = '' configurePhase = ''
export HOME=$TMPDIR export HOME=$TMPDIR
mkdir -p _site/style mkdir -p _site/style
# Copy node_modules instead of linking
cp -r ${nodeModules}/node_modules . cp -r ${nodeModules}/node_modules .
chmod -R +w node_modules chmod -R +w node_modules
cp ${packageJSON} package.json cp ${packageJSON} package.json
''; '';
buildPhase = '' buildPhase = ''
# Compile SCSS
echo 'Compiling SCSS' echo 'Compiling SCSS'
sass style/style.scss _site/style/style.css sass style/style.scss _site/style/style.css
# Minify CSS
echo 'Minifying CSS' echo 'Minifying CSS'
lightningcss --minify --targets '> 0.25%, not IE 11' _site/style/*.css -o _site/style/*.css lightningcss --minify --targets '> 0.25%, not IE 11' _site/style/*.css -o _site/style/*.css
# Build site with 11ty
echo 'Building site' echo 'Building site'
yarn eleventy yarn --offline eleventy
# Minify HTML
echo 'Minifying HTML' echo 'Minifying HTML'
html-minifier --input-dir _site --output-dir _site --collapse-whitespace --file-ext html html-minifier --input-dir _site --output-dir _site --collapse-whitespace --file-ext html
''; '';
installPhase = '' installPhase = ''
# Create output directory and copy files
mkdir -p $out mkdir -p $out
cp -r _site/* $out/ cp -r _site/* $out/
# Clean up
rm -rf node_modules _site package.json rm -rf node_modules _site package.json
''; '';
dontFixup = true;
dontPatch = true;
} }

View file

@ -7,19 +7,45 @@ in
pkgs.mkShell { pkgs.mkShell {
buildInputs = with pkgs; [ buildInputs = with pkgs; [
yarn yarn
nodejs_22 sass
lightningcss
]; ];
shellHook = '' shellHook = ''
# Use the same package.json and node_modules as the build
rm -rf node_modules rm -rf node_modules
rm -rf package.json rm -rf package.json
ln -sf ${packageJSON} package.json ln -sf ${packageJSON} package.json
ln -sf ${nodeModules}/node_modules . 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() { upgrade_deps() {
local mode=''${1:-"minor"} # default to minor updates local mode=''${1:-"minor"}
case $mode in case $mode in
"major") "major")
@ -48,17 +74,17 @@ pkgs.mkShell {
} }
export -f upgrade_deps export -f upgrade_deps
export -f serve
# Cleanup function
cleanup() { cleanup() {
echo "Cleaning up..." echo "Cleaning up..."
rm -rf node_modules package.json rm -rf node_modules _site package.json
} }
# Register the cleanup function to run when the shell exits
trap cleanup EXIT trap cleanup EXIT
echo "Development environment ready!" echo "Development environment ready!"
echo "Run 'serve' to start development server"
echo "Run 'upgrade_deps [major|minor|patch]' to upgrade your dependencies" echo "Run 'upgrade_deps [major|minor|patch]' to upgrade your dependencies"
''; '';
} }