Skip to content

Commit

Permalink
om hack: Initialize (#319)
Browse files Browse the repository at this point in the history
#316 

This doesn't do much right now, but renders the Markdown from
`om.hack.default.readme`. Spec will change in future iterations.

<img width="1058" alt="image"
src="https://github.com/user-attachments/assets/7514a389-eecc-418c-b444-5957f4518527">
  • Loading branch information
srid authored Oct 15, 2024
1 parent 4b83fda commit cfc87cc
Show file tree
Hide file tree
Showing 18 changed files with 229 additions and 2 deletions.
17 changes: 17 additions & 0 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"crates/omnix-common",
"crates/omnix-cli",
"crates/omnix-init",
"crates/omnix-hack",
"crates/nix_rs",
"crates/nixci",
"crates/nix_health",
Expand Down Expand Up @@ -40,6 +41,7 @@ nix_rs = { version = "1.0.0", path = "./crates/nix_rs" }
nonempty = { version = "0.10.0", features = ["serialize"] }
omnix-common = { version = "0.1.0", path = "./crates/omnix-common" }
omnix-init = { version = "0.1.0", path = "./crates/omnix-init" }
omnix-hack = { version = "0.1.0", path = "./crates/omnix-hack" }
os_info = "3.7.0"
reqwest = { version = "0.11", features = ["blocking", "json"] }
regex = "1.9.3"
Expand Down
4 changes: 2 additions & 2 deletions crates/nix_rs/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ static NIXCMD: OnceCell<NixCmd> = OnceCell::const_new();
/// rest of the instrumentation parameters.
#[instrument(name = "command")]
pub fn trace_cmd(cmd: &tokio::process::Command) {
trace_cmd_with("🐚", cmd);
trace_cmd_with("", cmd);
}

/// Like [trace_cmd] but with a custom icon
#[instrument(name = "command")]
pub fn trace_cmd_with(icon: &str, cmd: &tokio::process::Command) {
use colored::Colorize;
tracing::info!("{} {}️", icon, to_cli(cmd).bright_blue());
tracing::info!("{} {}️", icon, to_cli(cmd).dimmed());
}

impl NixCmd {
Expand Down
1 change: 1 addition & 0 deletions crates/omnix-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ nix_health = { workspace = true }
nix_rs = { workspace = true }
omnix-common = { workspace = true }
omnix-init = { workspace = true }
omnix-hack = { workspace = true }
tabled = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
Expand Down
3 changes: 3 additions & 0 deletions crates/omnix-cli/src/command/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pub enum Command {

Init(super::init::InitCommand),

Hack(super::hack::HackCommand),

CI(super::ci::CICommand),

Health(super::health::HealthCommand),
Expand All @@ -24,6 +26,7 @@ impl Command {
match self {
Command::Show(cmd) => cmd.run().await,
Command::Init(cmd) => cmd.run().await,
Command::Hack(cmd) => cmd.run().await,
Command::CI(cmd) => cmd.run(verbosity).await,
Command::Health(cmd) => cmd.run().await,
Command::Completion(cmd) => cmd.run(),
Expand Down
12 changes: 12 additions & 0 deletions crates/omnix-cli/src/command/hack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use clap::Parser;

/// Prepare to hack on a flake project
#[derive(Parser, Debug)]
pub struct HackCommand {}

impl HackCommand {
pub async fn run(&self) -> anyhow::Result<()> {
omnix_hack::core::hack_on().await?;
Ok(())
}
}
1 change: 1 addition & 0 deletions crates/omnix-cli/src/command/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod ci;
pub mod completion;
pub mod core;
pub mod hack;
pub mod health;
pub mod init;
pub mod show;
26 changes: 26 additions & 0 deletions crates/omnix-hack/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
authors = ["Sridhar Ratnakumar <[email protected]>"]
edition = "2021"
# If you change the name here, you must also do it in flake.nix (and run `cargo generate-lockfile` afterwards)
name = "omnix-hack"
version = "0.1.0"
repository = "https://github.com/juspay/omnix"
description = "Implementation for the `om hack` command"
license = "Apache-2.0"

[lib]
crate-type = ["cdylib", "rlib"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = { workspace = true }
lazy_static = { workspace = true }
nix_rs = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
syntect = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
omnix-common = { workspace = true }
24 changes: 24 additions & 0 deletions crates/omnix-hack/crate.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{ flake
, pkgs
, lib
, rust-project
, ...
}:

{
autoWire = lib.optionals
(lib.elem pkgs.system [ "x86_64-linux" "aarch64-darwin" ])
[ "doc" "clippy" ];
crane.args = {
buildInputs = lib.optionals pkgs.stdenv.isDarwin (
with pkgs.apple_sdk_frameworks; [
IOKit
]
);
inherit (rust-project.crates."nix_rs".crane.args)
DEFAULT_FLAKE_SCHEMAS
INSPECT_FLAKE
NIX_SYSTEMS
;
};
}
40 changes: 40 additions & 0 deletions crates/omnix-hack/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use serde::Deserialize;

use nix_rs::{command::NixCmd, flake::url::FlakeUrl};
use omnix_common::config::OmConfig;

use crate::readme::Readme;

#[derive(Debug, Deserialize, Clone)]
pub struct HackConfig {
pub cache: CacheConfig,
pub readme: Readme,
}

#[derive(Debug, Deserialize, Clone)]
pub struct CacheConfig {
pub cachix: CachixConfig,
}

#[derive(Debug, Deserialize, Clone)]
pub struct CachixConfig {
/// If enabled, configure environment to use the cache.
pub enable: bool,
/// Name of the cachix cache (`https://<name>.cachix.org`)
pub name: String,
/// The read-only auth token to use if this is a private cache
///
/// If provided, will run `cachix authtoken <auth_token>`.
pub auth_token: Option<String>,
}

impl HackConfig {
pub async fn from_flake(url: &FlakeUrl) -> anyhow::Result<Self> {
let v = OmConfig::<Self>::from_flake_url(NixCmd::get().await, url, &["om.hack"])
.await?
.config;
v.get("default")
.cloned()
.ok_or_else(|| anyhow::anyhow!("Missing key default for om.hack"))
}
}
20 changes: 20 additions & 0 deletions crates/omnix-hack/src/core.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::path::Path;

use nix_rs::flake::url::FlakeUrl;
use omnix_common::markdown::print_markdown;

use crate::config::HackConfig;

pub async fn hack_on() -> anyhow::Result<()> {
let here_flake: FlakeUrl = Into::<FlakeUrl>::into(Path::new("."));
let cfg = HackConfig::from_flake(&here_flake).await?;

// TODO: cachix check
// TODO: `om health`

let pwd = std::env::current_dir()?;
eprintln!();
print_markdown(&pwd, &cfg.readme.get_markdown()).await?;

Ok(())
}
3 changes: 3 additions & 0 deletions crates/omnix-hack/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod config;
pub mod core;
pub mod readme;
38 changes: 38 additions & 0 deletions crates/omnix-hack/src/readme.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use serde::Deserialize;

// TODO(idea): What if we provide `om health` like checkmark for each item. Automatically check if the user is in Nix shell or direnv, and ✅ the title accordingly. If not, nudge them to do it.
const OM_SHELL: &str = r#"## Enter the Nix shell
We recommend that you setup nix-direnv (a convenient template provided at <https://github.com/juspay/nixos-unified-template>), and then run the following in the project terminal to activate the Nix shell:
```sh-session
direnv allow
```
From this point, anytime you `cd` to this project directory, the Nix shell will be automatically activated.
"#;

const OM_IDE: &str = r#"## IDE or editor setup
>[!IMPORTANT] ❗Make sure you have setup `direnv` as stated above.
You can now launch your favourite editor or IDE from inside the Nix devshell. For VSCode in particular, consult <https://nixos.asia/en/vscode>.
"#;

/// The README to display at the end.
///
/// Placeholder parameters:
/// - `OM_SHELL`: Instructions to enter the Nix shell.
/// - `OM_IDE`: Instructions to setup the IDE.
#[derive(Debug, Deserialize, Clone)]
pub struct Readme(pub String);

impl Readme {
/// Get the Markdown string, after doing parameter replacements.
pub fn get_markdown(&self) -> String {
self.0
.replace("OM_SHELL", OM_SHELL)
.replace("OM_IDE", OM_IDE)
}
}
1 change: 1 addition & 0 deletions doc/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [CLI](om/index.md)
- [Show](om/show.md)
- [Health](om/health.md)
- [Hack](om/hack.md)
- [CI](om/ci.md)
- [Init](om/init.md)

Expand Down
1 change: 1 addition & 0 deletions doc/src/history.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Enhancements

- `om hack`: New command
- `om init`
- Initial working version of `om init` command
- `om health`
Expand Down
4 changes: 4 additions & 0 deletions doc/src/om/hack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Hack

> [!TODO]
> `om hack` is a work in progress
3 changes: 3 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ alias f := fmt
watch *ARGS:
bacon --job run -- -- {{ ARGS }}

run *ARGS:
cargo run -p omnix-cli {{ ARGS }}

alias w := watch

# Run CI locally
Expand Down
31 changes: 31 additions & 0 deletions nix/modules/om.nix
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,37 @@
# min_disk_space = "2T";
};
};
hack.default = {
# TODO: This is not implemented yet.
cache.cachix = {
enable = true;
name = "om";
# authToken = "xxx";
};
readme = ''
🍾 Welcome to the **omnix** project
OM_SHELL
OM_IDE
## Running `omnix` inside devShell
This will run `cargo watch` and run the resultant program, and then restart the same as you modify the Rust sources:
```sh-session
just watch <args>
```
## Running `omnix` through Nix
```sh-session
nix --accept-flake-config run github:juspay/omnix
```
## Read more
For details, see [README.md](README.md)
'';
};
};
};
}

0 comments on commit cfc87cc

Please sign in to comment.