Skip to content

Commit

Permalink
devmode: make reusable
Browse files Browse the repository at this point in the history
the `devmode` helper made for the Nixpkgs/NixOS manual was exposed wrapped
in `mkShell`, which made it impossible to reuse.

this change strips that wrapper and reproduces it at the call site.

now one can use `devmode` from anywhere Nixpkgs is available:

    devmode = pkgs.callPackage "${pkgs.path}/pkgs/tools/nix/web-devmode.nix" {
      buildArgs = toString ./.;
      open = "/index.html";
    };
  • Loading branch information
fricklerhandwerk committed Nov 12, 2024
1 parent 9d48107 commit b94f54b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 48 deletions.
13 changes: 9 additions & 4 deletions doc/doc-support/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
lib,
stdenvNoCC,
callPackage,
mkShellNoCC,
documentation-highlighter,
nixos-render-docs,
nixpkgs ? { },
Expand Down Expand Up @@ -95,10 +96,14 @@ stdenvNoCC.mkDerivation (

pythonInterpreterTable = callPackage ./python-interpreter-table.nix { };

shell = callPackage ../../pkgs/tools/nix/web-devmode.nix {
buildArgs = "./.";
open = "/share/doc/nixpkgs/manual.html";
};
shell =
let
devmode = callPackage ../../pkgs/tools/nix/web-devmode.nix {
buildArgs = "./.";
open = "/share/doc/nixpkgs/manual.html";
};
in
mkShellNoCC { packages = [ devmode ]; };

tests.manpage-urls = callPackage ../tests/manpage-urls.nix { };
};
Expand Down
9 changes: 6 additions & 3 deletions nixos/doc/manual/shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ let

common = import ./common.nix;
inherit (common) outputPath indexPath;
devmode = pkgs.callPackage ../../../pkgs/tools/nix/web-devmode.nix {
buildArgs = "../../release.nix -A manualHTML.${builtins.currentSystem}";
open = "/${outputPath}/${indexPath}";
};
in
pkgs.callPackage ../../../pkgs/tools/nix/web-devmode.nix {
buildArgs = "../../release.nix -A manualHTML.${builtins.currentSystem}";
open = "/${outputPath}/${indexPath}";
pkgs.mkShellNoCC {
packages = [ devmode ];
}
78 changes: 37 additions & 41 deletions pkgs/tools/nix/web-devmode.nix
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{
lib,
findutils,
mkShell,
nodejs_latest,
parallel,
rsync,
watchexec,
writeShellScriptBin,
# arguments to `nix-build`, e.g. `"foo.nix -A bar"`
buildArgs,
buildArgs ? "",
# what path to open a browser at
open,
open ? "/index.html",
}:
let
inherit (nodejs_latest.pkgs) live-server;

error_page = writeShellScriptBin "error_page" ''
echo "<!DOCTYPE html>
error-page = writeShellScriptBin "error-page" ''
cat << EOF
<!DOCTYPE html>
<html>
<head>
<style>
Expand All @@ -26,7 +26,8 @@ let
</style>
</head>
<body><pre>$1</pre></body>
</html>"
</html>
EOF
'';

# The following would have been simpler:
Expand All @@ -38,7 +39,7 @@ let
# Using rsync here, instead of `cp`, to get as close to an atomic
# directory copy operation as possible. `--delay-updates` should
# also go towards that.
build_and_copy = writeShellScriptBin "build_and_copy" ''
build-and-copy = writeShellScriptBin "build-and-copy" ''
set -euxo pipefail
set +e
Expand All @@ -49,7 +50,7 @@ let
if [ $exit_status -eq 0 ];
then
# setting permissions to be able to clean up
${lib.getBin rsync}/bin/rsync \
${lib.getExe rsync} \
--recursive \
--chmod=u=rwX \
--delete-before \
Expand All @@ -58,10 +59,10 @@ let
$serve/
else
set +x
${lib.getBin error_page}/bin/error_page "$stderr" > $error_page_absolute
${lib.getExe error-page} "$stderr" > $error_page_absolute
set -x
${lib.getBin findutils}/bin/find $serve \
${lib.getExe findutils} $serve \
-type f \
! -name $error_page_relative \
-delete
Expand All @@ -72,55 +73,50 @@ let
watcher = writeShellScriptBin "watcher" ''
set -euxo pipefail
${lib.getBin watchexec}/bin/watchexec \
${lib.getExe watchexec} \
--shell=none \
--restart \
--print-events \
${lib.getBin build_and_copy}/bin/build_and_copy
${lib.getExe build-and-copy}
'';

# A Rust alternative to live-server exists, but it was not in nixpkgs.
# A Rust alternative to live-server exists, but it fails to open the temporary directory.
# `--no-css-inject`: without this it seems that only CSS is auto-reloaded.
# https://www.npmjs.com/package/live-server
server = writeShellScriptBin "server" ''
set -euxo pipefail
${lib.getBin live-server}/bin/live-server \
${lib.getExe' live-server "live-server"} \
--host=127.0.0.1 \
--verbose \
--no-css-inject \
--entry-file=$error_page_relative \
--open=${open} \
$serve
'';
in
writeShellScriptBin "devmode" ''
set -euxo pipefail
devmode = writeShellScriptBin "devmode" ''
set -euxo pipefail
function handle_exit {
rm -rf "$tmpdir"
}
function handle_exit {
rm -rf "$tmpdir"
}
tmpdir=$(mktemp -d)
trap handle_exit EXIT
tmpdir=$(mktemp -d)
trap handle_exit EXIT
export out_link="$tmpdir/result"
export serve="$tmpdir/serve"
mkdir $serve
export error_page_relative=error.html
export error_page_absolute=$serve/$error_page_relative
${lib.getBin error_page}/bin/error_page "building …" > $error_page_absolute
export out_link="$tmpdir/result"
export serve="$tmpdir/serve"
mkdir $serve
export error_page_relative=error.html
export error_page_absolute=$serve/$error_page_relative
${lib.getExe error-page} "building …" > $error_page_absolute
${lib.getBin parallel}/bin/parallel \
--will-cite \
--line-buffer \
--tagstr '{/}' \
::: \
"${lib.getBin watcher}/bin/watcher" \
"${lib.getBin server}/bin/server"
'';
in
mkShell {
name = "web-devmode";
packages = [ devmode ];
}
${lib.getExe parallel} \
--will-cite \
--line-buffer \
--tagstr '{/}' \
::: \
"${lib.getExe watcher}" \
"${lib.getExe server}"
''

0 comments on commit b94f54b

Please sign in to comment.