Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add cargo-doc-live #246

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
/example/share-services/pgweb/data/
/example/simple/data/
/example/grafana-tempo/data/
/example/cargo-doc-live/target

/.pre-commit-config.yaml
40 changes: 40 additions & 0 deletions doc/cargo-doc-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# cargo-doc-live

[cargo-doc-live] is live server version of `cargo doc`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use the 1st paragraph and video from https://github.com/srid/cargo-doc-live in here?


{#start}

## Getting started

```nix
# In `perSystem.process-compose.<name>`
{
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know our docs elsewhere are more of skeleton, but we should get into the practice of fleshing out when writing new docs. Can you add some paragraphs to all these sections?


```nix
{
services.cargo-doc-live."cargo-doc-live1" = {
enable = true;
crateName = "chrono";
};
}
```
3 changes: 2 additions & 1 deletion doc/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]]#
Expand All @@ -25,5 +25,6 @@ short-title: Services
- [[prometheus]]#
- [[cassandra]]#
- [[weaviate]]#
- [[cargo-doc-live]]#

[gh]: https://github.com/juspay/services-flake/issues/132
7 changes: 7 additions & 0 deletions example/cargo-doc-live/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions example/cargo-doc-live/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[package]
name = "test"
106 changes: 106 additions & 0 deletions example/cargo-doc-live/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions example/cargo-doc-live/flake.nix
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a ./example/cargo-doc-live/README.md that explains how to run it?

Original file line number Diff line number Diff line change
@@ -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" = { ... }:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
process-compose."default" = { ... }:
process-compose."cargo-doc-live" = { ... }:

(The default package is usually the Rust package itself)

{
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";
};
};
};
};
}
3 changes: 3 additions & 0 deletions example/cargo-doc-live/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
83 changes: 83 additions & 0 deletions nix/cargo-doc-live.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{ pkgs, lib, name, config, ... }:
let
inherit (lib) types;
in
{
options = {
enable = lib.mkEnableOption name;

src = lib.mkOption {
type = types.path;
description = "The src of the cargo";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a default value.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description = "The src of the cargo";
description = "Path to the cargo project root";

};

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 "${config.src}/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 =
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The { in line 34 is indented inconsistently with the rest of the project (e.g.: see { in line 32). You should fix this in similar other places as well.

"${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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these are in runtimeInputs already, we don't need these and can just reference the executables as is in the script text, no?

''
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 = 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 \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
${lib.getExe pkgs.nodePackages.browser-sync} start --port ${toString config.port} --ss target/doc -s target/doc \
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";
};
};
};
};
};
}
1 change: 1 addition & 0 deletions nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ in
./tempo.nix
./weaviate.nix
./searxng.nix
./cargo-doc-live.nix
];
}