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;
nativeBuildInputs = with pkgs; [
sass
lightningcss
nodejs_22
yarn
cacert
curl
glib
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 = ''
export HOME=$TMPDIR
mkdir -p _site/style
# Copy node_modules instead of linking
cp -r ${nodeModules}/node_modules .
chmod -R +w node_modules
cp ${packageJSON} package.json
'';
buildPhase = ''
# Compile SCSS
echo 'Compiling SCSS'
sass style/style.scss _site/style/style.css
# Minify CSS
echo 'Minifying CSS'
lightningcss --minify --targets '> 0.25%, not IE 11' _site/style/*.css -o _site/style/*.css
# Build site with 11ty
echo 'Building site'
yarn eleventy
yarn --offline eleventy
# Minify HTML
echo 'Minifying HTML'
html-minifier --input-dir _site --output-dir _site --collapse-whitespace --file-ext html
'';
installPhase = ''
# Create output directory and copy files
mkdir -p $out
cp -r _site/* $out/
# Clean up
rm -rf node_modules _site package.json
'';
dontFixup = true;
dontPatch = true;
}

View file

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