From 7e2eb0943215c8d422deb6a3ead4d56e372ee9dd Mon Sep 17 00:00:00 2001 From: Hao Date: Sat, 22 Jun 2024 22:28:28 +1000 Subject: [PATCH 1/8] feat: Add cargo-doc-live --- doc/cargo-doc-live.md | 40 +++++++++++++++++++++ doc/services.md | 3 +- nix/cargo-doc-live.nix | 69 +++++++++++++++++++++++++++++++++++++ nix/cargo-doc-live_test.nix | 18 ++++++++++ nix/default.nix | 1 + test/flake.nix | 1 + 6 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 doc/cargo-doc-live.md create mode 100644 nix/cargo-doc-live.nix create mode 100644 nix/cargo-doc-live_test.nix diff --git a/doc/cargo-doc-live.md b/doc/cargo-doc-live.md new file mode 100644 index 00000000..b791b7f7 --- /dev/null +++ b/doc/cargo-doc-live.md @@ -0,0 +1,40 @@ +# cargo-doc-live + +[cargo-doc-live] is live server version of `cargo doc`. + +{#start} + +## Getting started + +```nix +# In `perSystem.process-compose.` +{ + services.cargo-doc-live."cargo-doc-live1".enable = true; +} +``` + +{#port} + +### The port for `cargo doc` + +```nix +{ + services.cargo-doc-live."cargo-doc-live1" = { + enable = true; + port = 8080; + }; +} +``` + +{#crateName} + +### The crate to use when opening docs in browser + +```nix +{ + services.cargo-doc-live."cargo-doc-live1" = { + enable = true; + crateName = "chrono"; + }; +} +``` diff --git a/doc/services.md b/doc/services.md index cec51d9a..5364488d 100644 --- a/doc/services.md +++ b/doc/services.md @@ -4,7 +4,7 @@ short-title: Services # Supported services ->[!warning] WIP +> [!warning] WIP > Documentation for the services is still in progress. Please refer to [this issue][gh] for progress. - [[apache-kafka]]# @@ -25,5 +25,6 @@ short-title: Services - [[prometheus]]# - [[cassandra]]# - [[weaviate]]# +- [[cargo-doc-live]]# [gh]: https://github.com/juspay/services-flake/issues/132 diff --git a/nix/cargo-doc-live.nix b/nix/cargo-doc-live.nix new file mode 100644 index 00000000..12d8a833 --- /dev/null +++ b/nix/cargo-doc-live.nix @@ -0,0 +1,69 @@ +{ pkgs, lib, name, config, ... }: +let + inherit (lib) types; +in +{ + options = { + enable = lib.mkEnableOption name; + + port = lib.mkOption { + type = types.port; + description = "The port for 'cargo doc'"; + default = 8008; + }; + + crateName = lib.mkOption { + type = types.str; + description = "The crate to use when opening docs in browser"; + default = builtins.replaceStrings [ "-" ] [ "_" ] + ((lib.trivial.importTOML ./Cargo.toml).package.name); + defaultText = "The crate name is derived from the Cargo.toml file"; + }; + + outputs.settings = lib.mkOption { + type = types.deferredModule; + internal = true; + readOnly = true; + default = { + processes = + let + browser-sync = lib.getExe pkgs.nodePackages.browser-sync; + cargo-watch = lib.getExe pkgs.cargo-watch; + cargo = lib.getExe pkgs.cargo; + in + { + "${name}-cargo-doc" = { + command = builtins.toString (pkgs.writeShellScript "cargo-doc" '' + run-cargo-doc() { + ${cargo} doc --document-private-items --all-features + ${browser-sync} reload --port ${toString config.port} # Trigger reload in browser + }; export -f run-cargo-doc + ${cargo-watch} watch -s run-cargo-doc + ''); + + readiness_probe = { + period_seconds = 1; + failure_threshold = 100000; # 'cargo doc' can take quite a while. + exec.command = '' + # Wait for the first 'cargo doc' to have completed. + # We'll use this state to block browser-sync from starting + # and opening the URL in the browser. + ls target/doc/${config.crateName}/index.html + ''; + }; + namespace = name; + availability.restart = "on_failure"; + }; + "${name}-browser-sync" = { + command = '' + ${browser-sync} start --port ${toString config.port} --ss target/doc -s target/doc \ + --startPath /${config.crateName}/ + ''; + namespace = name; + depends_on."${name}-cargo-doc".condition = "process_healthy"; + }; + }; + }; + }; + }; +} diff --git a/nix/cargo-doc-live_test.nix b/nix/cargo-doc-live_test.nix new file mode 100644 index 00000000..eb51c6a4 --- /dev/null +++ b/nix/cargo-doc-live_test.nix @@ -0,0 +1,18 @@ +{ pkgs, ... }: { + services.cargo-doc-live."cargo-doc-live1" = { + enable = true; + crateName = "simple"; + }; + + settings.processes.test = + { + command = pkgs.writeShellApplication { + runtimeInputs = [ pkgs.curl ]; + text = '' + curl http://127.0.0.1:8008/simple + ''; + name = "cargo-doc-live-test"; + }; + depends_on."cargo-doc-live1".condition = "process_healthy"; + }; +} diff --git a/nix/default.nix b/nix/default.nix index 1adbd7c3..45e89d24 100644 --- a/nix/default.nix +++ b/nix/default.nix @@ -22,5 +22,6 @@ in ./tempo.nix ./weaviate.nix ./searxng.nix + ./cargo-doc-live.nix ]; } diff --git a/test/flake.nix b/test/flake.nix index 1b8386f6..7bc7a035 100644 --- a/test/flake.nix +++ b/test/flake.nix @@ -49,6 +49,7 @@ "${inputs.services-flake}/nix/cassandra_test.nix" "${inputs.services-flake}/nix/tempo_test.nix" "${inputs.services-flake}/nix/weaviate_test.nix" + "${inputs.services-flake}/nix/cargo-doc-live_test.nix" ] ++ lib.optionals pkgs.stdenv.isLinux [ # Broken on Darwin: https://github.com/NixOS/nixpkgs/issues/316954 "${inputs.services-flake}/nix/grafana_test.nix" From 109a6db3a76519a26186884aca60c763c171260c Mon Sep 17 00:00:00 2001 From: Hao Date: Sun, 23 Jun 2024 09:16:07 +1000 Subject: [PATCH 2/8] Add missing inputs --- .gitignore | 2 + nix/cargo-doc-live/Cargo.lock | 7 +++ nix/cargo-doc-live/Cargo.toml | 2 + .../cargo-doc-live_test.nix | 0 .../default.nix} | 43 +++++++++++-------- nix/cargo-doc-live/src/main.rs | 3 ++ nix/default.nix | 2 +- test/flake.nix | 2 +- 8 files changed, 42 insertions(+), 19 deletions(-) create mode 100644 nix/cargo-doc-live/Cargo.lock create mode 100644 nix/cargo-doc-live/Cargo.toml rename nix/{ => cargo-doc-live}/cargo-doc-live_test.nix (100%) rename nix/{cargo-doc-live.nix => cargo-doc-live/default.nix} (55%) create mode 100644 nix/cargo-doc-live/src/main.rs diff --git a/.gitignore b/.gitignore index fb0b6e93..5bfded21 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,8 @@ # created when `just run ` is used /test/data +/nix/cargo-doc-live/target + # created when `just run ex-*` is used /example/share-services/pgweb/data/ /example/simple/data/ diff --git a/nix/cargo-doc-live/Cargo.lock b/nix/cargo-doc-live/Cargo.lock new file mode 100644 index 00000000..a56d7e2c --- /dev/null +++ b/nix/cargo-doc-live/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "simple" +version = "0.0.0" diff --git a/nix/cargo-doc-live/Cargo.toml b/nix/cargo-doc-live/Cargo.toml new file mode 100644 index 00000000..f6ae897e --- /dev/null +++ b/nix/cargo-doc-live/Cargo.toml @@ -0,0 +1,2 @@ +[package] +name = "simple" diff --git a/nix/cargo-doc-live_test.nix b/nix/cargo-doc-live/cargo-doc-live_test.nix similarity index 100% rename from nix/cargo-doc-live_test.nix rename to nix/cargo-doc-live/cargo-doc-live_test.nix diff --git a/nix/cargo-doc-live.nix b/nix/cargo-doc-live/default.nix similarity index 55% rename from nix/cargo-doc-live.nix rename to nix/cargo-doc-live/default.nix index 12d8a833..dd474c98 100644 --- a/nix/cargo-doc-live.nix +++ b/nix/cargo-doc-live/default.nix @@ -26,21 +26,25 @@ in readOnly = true; default = { processes = - let - browser-sync = lib.getExe pkgs.nodePackages.browser-sync; - cargo-watch = lib.getExe pkgs.cargo-watch; - cargo = lib.getExe pkgs.cargo; - in { "${name}-cargo-doc" = { - command = builtins.toString (pkgs.writeShellScript "cargo-doc" '' - run-cargo-doc() { - ${cargo} doc --document-private-items --all-features - ${browser-sync} reload --port ${toString config.port} # Trigger reload in browser - }; export -f run-cargo-doc - ${cargo-watch} watch -s run-cargo-doc - ''); - + command = pkgs.writeShellApplication { + name = "cargo-doc"; + runtimeInputs = with pkgs; [ cargo cargo-watch nodePackages.browser-sync ]; + text = + let + browser-sync = lib.getExe pkgs.nodePackages.browser-sync; + cargo-watch = lib.getExe pkgs.cargo-watch; + cargo = lib.getExe pkgs.cargo; + in + '' + run-cargo-doc() { + ${cargo} doc --document-private-items --all-features + ${browser-sync} reload --port ${toString config.port} # Trigger reload in browser + }; export -f run-cargo-doc + ${cargo-watch} watch -s run-cargo-doc + ''; + }; readiness_probe = { period_seconds = 1; failure_threshold = 100000; # 'cargo doc' can take quite a while. @@ -55,10 +59,15 @@ in availability.restart = "on_failure"; }; "${name}-browser-sync" = { - command = '' - ${browser-sync} start --port ${toString config.port} --ss target/doc -s target/doc \ - --startPath /${config.crateName}/ - ''; + command = pkgs.writeShellApplication { + name = "browser-sync"; + runtimeInputs = with pkgs; [ nodePackages.browser-sync ]; + text = + '' + ${lib.getExe pkgs.nodePackages.browser-sync} start --port ${toString config.port} --ss target/doc -s target/doc \ + --startPath /${config.crateName}/ + ''; + }; namespace = name; depends_on."${name}-cargo-doc".condition = "process_healthy"; }; diff --git a/nix/cargo-doc-live/src/main.rs b/nix/cargo-doc-live/src/main.rs new file mode 100644 index 00000000..e7a11a96 --- /dev/null +++ b/nix/cargo-doc-live/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/nix/default.nix b/nix/default.nix index 45e89d24..3828370d 100644 --- a/nix/default.nix +++ b/nix/default.nix @@ -22,6 +22,6 @@ in ./tempo.nix ./weaviate.nix ./searxng.nix - ./cargo-doc-live.nix + ./cargo-doc-live ]; } diff --git a/test/flake.nix b/test/flake.nix index 7bc7a035..fce64adf 100644 --- a/test/flake.nix +++ b/test/flake.nix @@ -49,7 +49,7 @@ "${inputs.services-flake}/nix/cassandra_test.nix" "${inputs.services-flake}/nix/tempo_test.nix" "${inputs.services-flake}/nix/weaviate_test.nix" - "${inputs.services-flake}/nix/cargo-doc-live_test.nix" + "${inputs.services-flake}/nix/cargo-doc-live/cargo-doc-live_test.nix" ] ++ lib.optionals pkgs.stdenv.isLinux [ # Broken on Darwin: https://github.com/NixOS/nixpkgs/issues/316954 "${inputs.services-flake}/nix/grafana_test.nix" From 3695662f7ff2b6563b5ba981ba92d318dad72156 Mon Sep 17 00:00:00 2001 From: Hao Date: Sun, 23 Jun 2024 20:58:06 +1000 Subject: [PATCH 3/8] fix: Move test to example --- .gitignore | 3 +- {nix => example}/cargo-doc-live/Cargo.lock | 2 +- example/cargo-doc-live/Cargo.toml | 2 + example/cargo-doc-live/flake.lock | 106 ++++++++++++++++++ example/cargo-doc-live/flake.nix | 44 ++++++++ {nix => example}/cargo-doc-live/src/main.rs | 0 .../default.nix => cargo-doc-live.nix} | 7 +- nix/cargo-doc-live/Cargo.toml | 2 - nix/cargo-doc-live/cargo-doc-live_test.nix | 18 --- nix/default.nix | 2 +- test/flake.nix | 1 - 11 files changed, 161 insertions(+), 26 deletions(-) rename {nix => example}/cargo-doc-live/Cargo.lock (89%) create mode 100644 example/cargo-doc-live/Cargo.toml create mode 100644 example/cargo-doc-live/flake.lock create mode 100644 example/cargo-doc-live/flake.nix rename {nix => example}/cargo-doc-live/src/main.rs (100%) rename nix/{cargo-doc-live/default.nix => cargo-doc-live.nix} (93%) delete mode 100644 nix/cargo-doc-live/Cargo.toml delete mode 100644 nix/cargo-doc-live/cargo-doc-live_test.nix diff --git a/.gitignore b/.gitignore index 5bfded21..b7fc8477 100644 --- a/.gitignore +++ b/.gitignore @@ -4,11 +4,10 @@ # created when `just run ` is used /test/data -/nix/cargo-doc-live/target - # created when `just run ex-*` is used /example/share-services/pgweb/data/ /example/simple/data/ /example/grafana-tempo/data/ +/example/cargo-doc-live/target /.pre-commit-config.yaml diff --git a/nix/cargo-doc-live/Cargo.lock b/example/cargo-doc-live/Cargo.lock similarity index 89% rename from nix/cargo-doc-live/Cargo.lock rename to example/cargo-doc-live/Cargo.lock index a56d7e2c..33b959e2 100644 --- a/nix/cargo-doc-live/Cargo.lock +++ b/example/cargo-doc-live/Cargo.lock @@ -3,5 +3,5 @@ version = 3 [[package]] -name = "simple" +name = "test" version = "0.0.0" diff --git a/example/cargo-doc-live/Cargo.toml b/example/cargo-doc-live/Cargo.toml new file mode 100644 index 00000000..40392766 --- /dev/null +++ b/example/cargo-doc-live/Cargo.toml @@ -0,0 +1,2 @@ +[package] +name = "test" diff --git a/example/cargo-doc-live/flake.lock b/example/cargo-doc-live/flake.lock new file mode 100644 index 00000000..e253a45b --- /dev/null +++ b/example/cargo-doc-live/flake.lock @@ -0,0 +1,106 @@ +{ + "nodes": { + "flake-parts": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1717285511, + "narHash": "sha256-iKzJcpdXih14qYVcZ9QC9XuZYnPc6T8YImb6dX166kw=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "2a55567fcf15b1b1c7ed712a2c6fadaec7412ea8", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1719082008, + "narHash": "sha256-jHJSUH619zBQ6WdC21fFAlDxHErKVDJ5fpN0Hgx4sjs=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "9693852a2070b398ee123a329e68f0dab5526681", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-lib": { + "locked": { + "lastModified": 1717284937, + "narHash": "sha256-lIbdfCsf8LMFloheeE6N31+BMIeixqyQWbSr2vk79EQ=", + "type": "tarball", + "url": "https://github.com/NixOS/nixpkgs/archive/eb9ceca17df2ea50a250b6b27f7bf6ab0186f198.tar.gz" + }, + "original": { + "type": "tarball", + "url": "https://github.com/NixOS/nixpkgs/archive/eb9ceca17df2ea50a250b6b27f7bf6ab0186f198.tar.gz" + } + }, + "process-compose-flake": { + "locked": { + "lastModified": 1718031437, + "narHash": "sha256-+RrlkAVZx0QhyeHAGFJnjST+/7Dc3zsDU3zAKXoDXaI=", + "owner": "Platonic-Systems", + "repo": "process-compose-flake", + "rev": "9344fac44edced4c686721686a6ad904d067c546", + "type": "github" + }, + "original": { + "owner": "Platonic-Systems", + "repo": "process-compose-flake", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-parts": "flake-parts", + "nixpkgs": "nixpkgs", + "process-compose-flake": "process-compose-flake", + "services-flake": "services-flake", + "systems": "systems" + } + }, + "services-flake": { + "locked": { + "lastModified": 1719102528, + "narHash": "sha256-wLoHpUJXVELcnFh7XgJ9m4q1Hu8WgGfrZQQHpS5eRcM=", + "owner": "juspay", + "repo": "services-flake", + "rev": "641937bc0f523405fbac4bb89c074de613dcee4d", + "type": "github" + }, + "original": { + "owner": "juspay", + "repo": "services-flake", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/example/cargo-doc-live/flake.nix b/example/cargo-doc-live/flake.nix new file mode 100644 index 00000000..9f68f558 --- /dev/null +++ b/example/cargo-doc-live/flake.nix @@ -0,0 +1,44 @@ +{ + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; + flake-parts.url = "github:hercules-ci/flake-parts"; + systems.url = "github:nix-systems/default"; + process-compose-flake.url = "github:Platonic-Systems/process-compose-flake"; + services-flake.url = "github:juspay/services-flake"; + }; + outputs = inputs: + inputs.flake-parts.lib.mkFlake { inherit inputs; } { + systems = import inputs.systems; + imports = [ + inputs.process-compose-flake.flakeModule + ]; + perSystem = { self', pkgs, lib, ... }: { + # `process-compose.foo` will add a flake package output called "foo". + # Therefore, this will add a default package that you can build using + # `nix build` and run using `nix run`. + process-compose."default" = { ... }: + { + imports = [ + inputs.services-flake.processComposeModules.default + ]; + + services.cargo-doc-live."cargo-doc-live1" = { + src = inputs.self; + enable = true; + port = 8009; + }; + + settings.processes.test = { + command = pkgs.writeShellApplication { + name = "cargo-doc-live-test"; + runtimeInputs = [ pkgs.curl ]; + text = '' + curl http://127.0.0.1:8009/test + ''; + }; + depends_on."cargo-doc-live1".condition = "process_healthy"; + }; + }; + }; + }; +} diff --git a/nix/cargo-doc-live/src/main.rs b/example/cargo-doc-live/src/main.rs similarity index 100% rename from nix/cargo-doc-live/src/main.rs rename to example/cargo-doc-live/src/main.rs diff --git a/nix/cargo-doc-live/default.nix b/nix/cargo-doc-live.nix similarity index 93% rename from nix/cargo-doc-live/default.nix rename to nix/cargo-doc-live.nix index dd474c98..e777902f 100644 --- a/nix/cargo-doc-live/default.nix +++ b/nix/cargo-doc-live.nix @@ -6,6 +6,11 @@ in options = { enable = lib.mkEnableOption name; + src = lib.mkOption { + type = types.path; + description = "The src of the cargo"; + }; + port = lib.mkOption { type = types.port; description = "The port for 'cargo doc'"; @@ -16,7 +21,7 @@ in type = types.str; description = "The crate to use when opening docs in browser"; default = builtins.replaceStrings [ "-" ] [ "_" ] - ((lib.trivial.importTOML ./Cargo.toml).package.name); + ((lib.trivial.importTOML "${config.src}/Cargo.toml").package.name); defaultText = "The crate name is derived from the Cargo.toml file"; }; diff --git a/nix/cargo-doc-live/Cargo.toml b/nix/cargo-doc-live/Cargo.toml deleted file mode 100644 index f6ae897e..00000000 --- a/nix/cargo-doc-live/Cargo.toml +++ /dev/null @@ -1,2 +0,0 @@ -[package] -name = "simple" diff --git a/nix/cargo-doc-live/cargo-doc-live_test.nix b/nix/cargo-doc-live/cargo-doc-live_test.nix deleted file mode 100644 index eb51c6a4..00000000 --- a/nix/cargo-doc-live/cargo-doc-live_test.nix +++ /dev/null @@ -1,18 +0,0 @@ -{ pkgs, ... }: { - services.cargo-doc-live."cargo-doc-live1" = { - enable = true; - crateName = "simple"; - }; - - settings.processes.test = - { - command = pkgs.writeShellApplication { - runtimeInputs = [ pkgs.curl ]; - text = '' - curl http://127.0.0.1:8008/simple - ''; - name = "cargo-doc-live-test"; - }; - depends_on."cargo-doc-live1".condition = "process_healthy"; - }; -} diff --git a/nix/default.nix b/nix/default.nix index 3828370d..45e89d24 100644 --- a/nix/default.nix +++ b/nix/default.nix @@ -22,6 +22,6 @@ in ./tempo.nix ./weaviate.nix ./searxng.nix - ./cargo-doc-live + ./cargo-doc-live.nix ]; } diff --git a/test/flake.nix b/test/flake.nix index fce64adf..1b8386f6 100644 --- a/test/flake.nix +++ b/test/flake.nix @@ -49,7 +49,6 @@ "${inputs.services-flake}/nix/cassandra_test.nix" "${inputs.services-flake}/nix/tempo_test.nix" "${inputs.services-flake}/nix/weaviate_test.nix" - "${inputs.services-flake}/nix/cargo-doc-live/cargo-doc-live_test.nix" ] ++ lib.optionals pkgs.stdenv.isLinux [ # Broken on Darwin: https://github.com/NixOS/nixpkgs/issues/316954 "${inputs.services-flake}/nix/grafana_test.nix" From 7877ddc635eb45ebe0b71f02bc529065f06b6c5f Mon Sep 17 00:00:00 2001 From: Hao Date: Mon, 24 Jun 2024 09:48:28 +1000 Subject: [PATCH 4/8] docs: address PR feedback - round 1 --- doc/cargo-doc-live.md | 5 +- example/cargo-doc-live/flake.nix | 4 +- flake.nix | 4 ++ nix/cargo-doc-live.nix | 87 +++++++++++++++----------------- 4 files changed, 51 insertions(+), 49 deletions(-) diff --git a/doc/cargo-doc-live.md b/doc/cargo-doc-live.md index b791b7f7..6ff04d3e 100644 --- a/doc/cargo-doc-live.md +++ b/doc/cargo-doc-live.md @@ -1,6 +1,8 @@ # cargo-doc-live -[cargo-doc-live] is live server version of `cargo doc`. +[cargo-doc-live] is live server version of `cargo doc` ― edit Rust code, and see the docs view in your web browser update automatically. + +https://github.com/srid/cargo-doc-live/assets/3998/37378858-dda1-40fb-8f6a-f76dc857a661 {#start} @@ -20,6 +22,7 @@ ```nix { services.cargo-doc-live."cargo-doc-live1" = { + projectRoot = ./.; enable = true; port = 8080; }; diff --git a/example/cargo-doc-live/flake.nix b/example/cargo-doc-live/flake.nix index 9f68f558..da0da92b 100644 --- a/example/cargo-doc-live/flake.nix +++ b/example/cargo-doc-live/flake.nix @@ -16,14 +16,14 @@ # `process-compose.foo` will add a flake package output called "foo". # Therefore, this will add a default package that you can build using # `nix build` and run using `nix run`. - process-compose."default" = { ... }: + process-compose."cargo-doc-live" = _: { imports = [ inputs.services-flake.processComposeModules.default ]; services.cargo-doc-live."cargo-doc-live1" = { - src = inputs.self; + projectRoot = inputs.self; enable = true; port = 8009; }; diff --git a/flake.nix b/flake.nix index 3dada658..f1bb27b0 100644 --- a/flake.nix +++ b/flake.nix @@ -28,6 +28,10 @@ }; dir = "./example/share-services/pgweb"; }; + cargo-doc-live-example = { + inherit overrideInputs; + dir = "./example/cargo-doc-live"; + }; test = { inherit overrideInputs; dir = "./test"; diff --git a/nix/cargo-doc-live.nix b/nix/cargo-doc-live.nix index e777902f..ba623bd9 100644 --- a/nix/cargo-doc-live.nix +++ b/nix/cargo-doc-live.nix @@ -6,9 +6,10 @@ in options = { enable = lib.mkEnableOption name; - src = lib.mkOption { + projectRoot = lib.mkOption { type = types.path; - description = "The src of the cargo"; + description = "Path to the cargo project root"; + default = ./.; }; port = lib.mkOption { @@ -21,7 +22,7 @@ in type = types.str; description = "The crate to use when opening docs in browser"; default = builtins.replaceStrings [ "-" ] [ "_" ] - ((lib.trivial.importTOML "${config.src}/Cargo.toml").package.name); + ((lib.trivial.importTOML "${config.projectRoot}/Cargo.toml").package.name); defaultText = "The crate name is derived from the Cargo.toml file"; }; @@ -30,53 +31,47 @@ in internal = true; readOnly = true; default = { - processes = - { - "${name}-cargo-doc" = { - command = pkgs.writeShellApplication { - name = "cargo-doc"; - runtimeInputs = with pkgs; [ cargo cargo-watch nodePackages.browser-sync ]; - text = - let - browser-sync = lib.getExe pkgs.nodePackages.browser-sync; - cargo-watch = lib.getExe pkgs.cargo-watch; - cargo = lib.getExe pkgs.cargo; - in - '' - run-cargo-doc() { - ${cargo} doc --document-private-items --all-features - ${browser-sync} reload --port ${toString config.port} # Trigger reload in browser - }; export -f run-cargo-doc - ${cargo-watch} watch -s run-cargo-doc - ''; - }; - readiness_probe = { - period_seconds = 1; - failure_threshold = 100000; # 'cargo doc' can take quite a while. - exec.command = '' - # Wait for the first 'cargo doc' to have completed. - # We'll use this state to block browser-sync from starting - # and opening the URL in the browser. - ls target/doc/${config.crateName}/index.html + processes = { + "${name}-cargo-doc" = { + command = pkgs.writeShellApplication { + name = "cargo-doc"; + runtimeInputs = with pkgs; [ cargo cargo-watch nodePackages.browser-sync ]; + text = + '' + run-cargo-doc() { + cargo doc --document-private-items --all-features + browser-sync reload --port ${toString config.port} # Trigger reload in browser + }; export -f run-cargo-doc + cargo-watch watch -s run-cargo-doc ''; - }; - namespace = name; - availability.restart = "on_failure"; }; - "${name}-browser-sync" = { - command = pkgs.writeShellApplication { - name = "browser-sync"; - runtimeInputs = with pkgs; [ nodePackages.browser-sync ]; - text = - '' - ${lib.getExe pkgs.nodePackages.browser-sync} start --port ${toString config.port} --ss target/doc -s target/doc \ - --startPath /${config.crateName}/ - ''; - }; - namespace = name; - depends_on."${name}-cargo-doc".condition = "process_healthy"; + readiness_probe = { + period_seconds = 1; + failure_threshold = 100000; # 'cargo doc' can take quite a while. + exec.command = '' + # Wait for the first 'cargo doc' to have completed. + # We'll use this state to block browser-sync from starting + # and opening the URL in the browser. + ls target/doc/${config.crateName}/index.html + ''; }; + namespace = name; + availability.restart = "on_failure"; }; + "${name}-browser-sync" = { + command = pkgs.writeShellApplication { + name = "browser-sync"; + runtimeInputs = [ pkgs.nodePackages.browser-sync ]; + text = + '' + browser-sync start --port ${toString config.port} --ss target/doc -s target/doc \ + --startPath /${config.crateName}/ + ''; + }; + namespace = name; + depends_on."${name}-cargo-doc".condition = "process_healthy"; + }; + }; }; }; }; From 6de6f59f7773bc7852d9737c49c3b09ea5b11e48 Mon Sep 17 00:00:00 2001 From: Hao Date: Mon, 24 Jun 2024 12:10:15 +1000 Subject: [PATCH 5/8] fix: add preHook to enter the correct directory --- example/cargo-doc-live/flake.nix | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/example/cargo-doc-live/flake.nix b/example/cargo-doc-live/flake.nix index da0da92b..7fe8d683 100644 --- a/example/cargo-doc-live/flake.nix +++ b/example/cargo-doc-live/flake.nix @@ -18,10 +18,24 @@ # `nix build` and run using `nix run`. process-compose."cargo-doc-live" = _: { + tui = false; + imports = [ inputs.services-flake.processComposeModules.default ]; + preHook = '' + # Set pipefail option for safer bash + set -euo pipefail + + # Copy project root to a mutable area + # We expect "command" to mutate it. + export HOME="$TMP" + cp -r ${inputs.self} "$HOME"/project + chmod -R a+w "$HOME"/project + cd "$HOME"/project + ''; + services.cargo-doc-live."cargo-doc-live1" = { projectRoot = inputs.self; enable = true; From 37459236b56d9fb4ef135eb881467c6979d5f1af Mon Sep 17 00:00:00 2001 From: Hao Date: Mon, 24 Jun 2024 12:25:05 +1000 Subject: [PATCH 6/8] docs: add more doc --- doc/cargo-doc-live.md | 7 ++++--- example/cargo-doc-live/README.md | 8 ++++++++ example/cargo-doc-live/flake.nix | 1 - 3 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 example/cargo-doc-live/README.md diff --git a/doc/cargo-doc-live.md b/doc/cargo-doc-live.md index 6ff04d3e..725c7698 100644 --- a/doc/cargo-doc-live.md +++ b/doc/cargo-doc-live.md @@ -17,13 +17,13 @@ https://github.com/srid/cargo-doc-live/assets/3998/37378858-dda1-40fb-8f6a-f76dc {#port} -### The port for `cargo doc` +### The port for `cargo doc`, the default value is 8008, while you could override it if 8008 is in use for another service. ```nix { services.cargo-doc-live."cargo-doc-live1" = { - projectRoot = ./.; enable = true; + projectRoot = ./.; port = 8080; }; } @@ -31,12 +31,13 @@ https://github.com/srid/cargo-doc-live/assets/3998/37378858-dda1-40fb-8f6a-f76dc {#crateName} -### The crate to use when opening docs in browser +### The crate to use when opening docs in browser, the crate name will be derived from Cargo.toml. ```nix { services.cargo-doc-live."cargo-doc-live1" = { enable = true; + projectRoot = ./.; crateName = "chrono"; }; } diff --git a/example/cargo-doc-live/README.md b/example/cargo-doc-live/README.md new file mode 100644 index 00000000..660666a9 --- /dev/null +++ b/example/cargo-doc-live/README.md @@ -0,0 +1,8 @@ +A demonstration of cargo-doc-live. + +To run, + +``` +cd ./example/cargo-doc-live +nix run .#cargo-doc-live +``` diff --git a/example/cargo-doc-live/flake.nix b/example/cargo-doc-live/flake.nix index 7fe8d683..c2d2057c 100644 --- a/example/cargo-doc-live/flake.nix +++ b/example/cargo-doc-live/flake.nix @@ -29,7 +29,6 @@ set -euo pipefail # Copy project root to a mutable area - # We expect "command" to mutate it. export HOME="$TMP" cp -r ${inputs.self} "$HOME"/project chmod -R a+w "$HOME"/project From 3fa26c030d4d1fd81ef0769f7b6fdcf7891e0190 Mon Sep 17 00:00:00 2001 From: Hao Date: Mon, 24 Jun 2024 16:41:01 +1000 Subject: [PATCH 7/8] fix: wait until browser-sync is ready --- example/cargo-doc-live/flake.nix | 5 +---- nix/cargo-doc-live.nix | 7 +++++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/example/cargo-doc-live/flake.nix b/example/cargo-doc-live/flake.nix index c2d2057c..b5c52783 100644 --- a/example/cargo-doc-live/flake.nix +++ b/example/cargo-doc-live/flake.nix @@ -13,9 +13,6 @@ inputs.process-compose-flake.flakeModule ]; perSystem = { self', pkgs, lib, ... }: { - # `process-compose.foo` will add a flake package output called "foo". - # Therefore, this will add a default package that you can build using - # `nix build` and run using `nix run`. process-compose."cargo-doc-live" = _: { tui = false; @@ -49,7 +46,7 @@ curl http://127.0.0.1:8009/test ''; }; - depends_on."cargo-doc-live1".condition = "process_healthy"; + depends_on."cargo-doc-live1-browser-sync".condition = "process_healthy"; }; }; }; diff --git a/nix/cargo-doc-live.nix b/nix/cargo-doc-live.nix index ba623bd9..b06338a6 100644 --- a/nix/cargo-doc-live.nix +++ b/nix/cargo-doc-live.nix @@ -68,6 +68,13 @@ in --startPath /${config.crateName}/ ''; }; + readiness_probe = { + period_seconds = 1; + failure_threshold = 10; + exec.command = '' + ps aux | grep browser-sync + ''; + }; namespace = name; depends_on."${name}-cargo-doc".condition = "process_healthy"; }; From 8a003faa189efd514a44984b975ac38f2315bee5 Mon Sep 17 00:00:00 2001 From: Hao Date: Mon, 24 Jun 2024 16:52:42 +1000 Subject: [PATCH 8/8] fix: change the way of checking readiness --- nix/cargo-doc-live.nix | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/nix/cargo-doc-live.nix b/nix/cargo-doc-live.nix index b06338a6..a3f6dcb1 100644 --- a/nix/cargo-doc-live.nix +++ b/nix/cargo-doc-live.nix @@ -69,11 +69,17 @@ in ''; }; readiness_probe = { - period_seconds = 1; - failure_threshold = 10; - exec.command = '' - ps aux | grep browser-sync - ''; + http_get = { + scheme = "http"; + host = "localhost"; + port = config.port; + path = config.crateName; + }; + initial_delay_seconds = 15; + period_seconds = 10; + timeout_seconds = 2; + success_threshold = 1; + failure_threshold = 5; }; namespace = name; depends_on."${name}-cargo-doc".condition = "process_healthy";