diff --git a/Cargo.lock b/Cargo.lock index 512e9236..fe3fd34c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -573,15 +573,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "coolor" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "691defa50318376447a73ced869862baecfab35f6aabaa91a4cd726b315bfe1a" -dependencies = [ - "crossterm 0.28.1", -] - [[package]] name = "core-foundation" version = "0.9.4" @@ -616,54 +607,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "crokey" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "520e83558f4c008ac06fa6a86e5c1d4357be6f994cce7434463ebcdaadf47bb1" -dependencies = [ - "crokey-proc_macros", - "crossterm 0.28.1", - "once_cell", - "serde", - "strict", -] - -[[package]] -name = "crokey-proc_macros" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "370956e708a1ce65fe4ac5bb7185791e0ece7485087f17736d54a23a0895049f" -dependencies = [ - "crossterm 0.28.1", - "proc-macro2", - "quote", - "strict", - "syn 1.0.109", -] - -[[package]] -name = "crossbeam" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1137cd7e7fc0fb5d3c5a8678be38ec56e819125d8d7907411fe24ccb943faca8" -dependencies = [ - "crossbeam-channel", - "crossbeam-deque", - "crossbeam-epoch", - "crossbeam-queue", - "crossbeam-utils", -] - -[[package]] -name = "crossbeam-channel" -version = "0.5.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33480d6946193aa8033910124896ca395333cae7e2d1113d1fef6c3272217df2" -dependencies = [ - "crossbeam-utils", -] - [[package]] name = "crossbeam-deque" version = "0.8.5" @@ -683,15 +626,6 @@ dependencies = [ "crossbeam-utils", ] -[[package]] -name = "crossbeam-queue" -version = "0.3.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df0346b5d5e76ac2fe4e327c5fd1118d6be7c51dfb18f9b7922923f287471e35" -dependencies = [ - "crossbeam-utils", -] - [[package]] name = "crossbeam-utils" version = "0.8.20" @@ -707,24 +641,8 @@ dependencies = [ "bitflags 1.3.2", "crossterm_winapi", "libc", - "mio 0.8.11", - "parking_lot", - "signal-hook", - "signal-hook-mio", - "winapi", -] - -[[package]] -name = "crossterm" -version = "0.28.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "829d955a0bb380ef178a640b91779e3987da38c9aea133b20614cfed8cdea9c6" -dependencies = [ - "bitflags 2.4.1", - "crossterm_winapi", - "mio 1.0.2", + "mio", "parking_lot", - "rustix", "signal-hook", "signal-hook-mio", "winapi", @@ -1450,7 +1368,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fddf93031af70e75410a2511ec04d49e758ed2f26dad3404a934e0fb45cc12a" dependencies = [ "bitflags 2.4.1", - "crossterm 0.25.0", + "crossterm", "dyn-clone", "fuzzy-matcher", "fxhash", @@ -1541,29 +1459,6 @@ dependencies = [ "smallvec", ] -[[package]] -name = "lazy-regex" -version = "3.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d8e41c97e6bc7ecb552016274b99fbb5d035e8de288c582d9b933af6677bfda" -dependencies = [ - "lazy-regex-proc_macros", - "once_cell", - "regex", -] - -[[package]] -name = "lazy-regex-proc_macros" -version = "3.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76e1d8b05d672c53cb9c7b920bbba8783845ae4f0b076e02a3db1d02c81b4163" -dependencies = [ - "proc-macro2", - "quote", - "regex", - "syn 2.0.53", -] - [[package]] name = "lazy_static" version = "1.4.0" @@ -1598,6 +1493,12 @@ version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" +[[package]] +name = "linked-hash-map" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" + [[package]] name = "linux-raw-sys" version = "0.4.14" @@ -1679,15 +1580,6 @@ dependencies = [ "unicase", ] -[[package]] -name = "minimad" -version = "0.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9c5d708226d186590a7b6d4a9780e2bdda5f689e0d58cd17012a298efd745d2" -dependencies = [ - "once_cell", -] - [[package]] name = "minimal-lexical" version = "0.2.1" @@ -1725,19 +1617,6 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "mio" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec" -dependencies = [ - "hermit-abi", - "libc", - "log", - "wasi", - "windows-sys 0.52.0", -] - [[package]] name = "native-tls" version = "0.2.12" @@ -1992,6 +1871,7 @@ dependencies = [ "nix_rs", "nixci", "omnix-common", + "omnix-hack", "omnix-init", "predicates", "regex", @@ -2010,6 +1890,7 @@ dependencies = [ "clap", "clap-verbosity-flag", "futures-lite", + "lazy_static", "nix_rs", "pulldown-cmark", "pulldown-cmark-mdcat", @@ -2022,6 +1903,22 @@ dependencies = [ "which", ] +[[package]] +name = "omnix-hack" +version = "0.1.0" +dependencies = [ + "anyhow", + "lazy_static", + "nix_rs", + "omnix-common", + "serde", + "serde_json", + "syntect", + "thiserror", + "tokio", + "tracing", +] + [[package]] name = "omnix-init" version = "0.1.0" @@ -2039,7 +1936,6 @@ dependencies = [ "omnix-common", "serde", "serde_json", - "termimad", "thiserror", "tokio", "tracing", @@ -2051,6 +1947,28 @@ version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" +[[package]] +name = "onig" +version = "6.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c4b31c8722ad9171c6d77d3557db078cab2bd50afcc9d09c8b315c59df8ca4f" +dependencies = [ + "bitflags 1.3.2", + "libc", + "once_cell", + "onig_sys", +] + +[[package]] +name = "onig_sys" +version = "69.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b829e3d7e9cc74c7e315ee8edb185bf4190da5acde74afd7fc59c35b1f086e7" +dependencies = [ + "cc", + "pkg-config", +] + [[package]] name = "openssl" version = "0.10.66" @@ -2865,8 +2783,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34db1a06d485c9142248b7a054f034b349b212551f3dfd19c94d45a754a217cd" dependencies = [ "libc", - "mio 0.8.11", - "mio 1.0.2", + "mio", "signal-hook", ] @@ -2952,12 +2869,6 @@ dependencies = [ "lock_api", ] -[[package]] -name = "strict" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f42444fea5b87a39db4218d9422087e66a85d0e7a0963a439b07bcdf91804006" - [[package]] name = "strict-num" version = "0.1.1" @@ -3029,6 +2940,7 @@ dependencies = [ "flate2", "fnv", "once_cell", + "onig", "plist", "regex-syntax 0.8.2", "serde", @@ -3036,6 +2948,7 @@ dependencies = [ "serde_json", "thiserror", "walkdir", + "yaml-rust", ] [[package]] @@ -3130,22 +3043,6 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "termimad" -version = "0.30.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "920e7c4671e79f3d9df269da9c8edf0dbc580044fd727d3594f7bfba5eb6107a" -dependencies = [ - "coolor", - "crokey", - "crossbeam", - "lazy-regex", - "minimad", - "serde", - "thiserror", - "unicode-width", -] - [[package]] name = "terminal_size" version = "0.3.0" @@ -3294,7 +3191,7 @@ dependencies = [ "backtrace", "bytes", "libc", - "mio 0.8.11", + "mio", "num_cpus", "parking_lot", "pin-project-lite", @@ -3991,6 +3888,15 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec7a2a501ed189703dba8b08142f057e887dfc4b2cc4db2d343ac6376ba3e0b9" +[[package]] +name = "yaml-rust" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" +dependencies = [ + "linked-hash-map", +] + [[package]] name = "zerocopy" version = "0.7.35" diff --git a/Cargo.toml b/Cargo.toml index f01ca2f4..0f81e65e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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", @@ -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" @@ -50,7 +52,7 @@ serde_repr = "0.1.18" serde_with = { version = "3.2", features = ["json"] } shell-words = { version = "1.1.0" } sysinfo = "0.29.10" -syntect = { version = "5.2.0", default-features = false } +syntect = { version = "5.2.0", features = ["default-syntaxes"] } tabled = "0.15" tempfile = "3" termimad = "0.30.0" diff --git a/crates/omnix-cli/Cargo.toml b/crates/omnix-cli/Cargo.toml index c9031fda..f5f744dd 100644 --- a/crates/omnix-cli/Cargo.toml +++ b/crates/omnix-cli/Cargo.toml @@ -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 } diff --git a/crates/omnix-cli/src/command/core.rs b/crates/omnix-cli/src/command/core.rs index 19740f6b..f85c5e08 100644 --- a/crates/omnix-cli/src/command/core.rs +++ b/crates/omnix-cli/src/command/core.rs @@ -7,6 +7,8 @@ pub enum Command { Init(super::init::InitCommand), + Hack(super::hack::HackCommand), + CI(super::ci::CICommand), Health(super::health::HealthCommand), @@ -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(), diff --git a/crates/omnix-cli/src/command/hack.rs b/crates/omnix-cli/src/command/hack.rs new file mode 100644 index 00000000..8b885aa9 --- /dev/null +++ b/crates/omnix-cli/src/command/hack.rs @@ -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(()) + } +} diff --git a/crates/omnix-cli/src/command/mod.rs b/crates/omnix-cli/src/command/mod.rs index 8fa877a4..fddbcc37 100644 --- a/crates/omnix-cli/src/command/mod.rs +++ b/crates/omnix-cli/src/command/mod.rs @@ -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; diff --git a/crates/omnix-common/Cargo.toml b/crates/omnix-common/Cargo.toml index 7bf259e6..e5419d65 100644 --- a/crates/omnix-common/Cargo.toml +++ b/crates/omnix-common/Cargo.toml @@ -18,6 +18,7 @@ futures-lite = { workspace = true } nix_rs = { workspace = true } pulldown-cmark-mdcat = { workspace = true } pulldown-cmark = { workspace = true } +lazy_static = { workspace = true } serde = { workspace = true } syntect = { workspace = true } thiserror = { workspace = true } diff --git a/crates/omnix-common/src/markdown.rs b/crates/omnix-common/src/markdown.rs index 370261f2..1b1bb58b 100644 --- a/crates/omnix-common/src/markdown.rs +++ b/crates/omnix-common/src/markdown.rs @@ -1,4 +1,5 @@ //! Markdown rendering using `mdcat` +use lazy_static::lazy_static; use pulldown_cmark::{Options, Parser}; use pulldown_cmark_mdcat::{ resources::FileResourceHandler, Environment, Settings, TerminalProgram, TerminalSize, Theme, @@ -6,21 +7,22 @@ use pulldown_cmark_mdcat::{ use std::path::Path; use syntect::parsing::SyntaxSet; -/// Print Markdown using `mdcat` to STDERR -pub async fn print_markdown(base_dir: &Path, s: &str) -> anyhow::Result<()> { - // Create a new environment for rendering - let env = Environment::for_local_directory(&base_dir)?; +lazy_static! { + static ref SYNTAX_SET: SyntaxSet = SyntaxSet::load_defaults_newlines(); - // Create default settings - let settings = Settings { + /// Global settings for rendering markdown + pub static ref SETTINGS: Settings<'static> = Settings { terminal_capabilities: TerminalProgram::detect().capabilities(), - terminal_size: TerminalSize::default(), + terminal_size: TerminalSize::from_terminal().unwrap_or_default(), theme: Theme::default(), - syntax_set: &SyntaxSet::default(), + syntax_set: &SYNTAX_SET, }; +} +/// Print Markdown using `mdcat` to STDERR +pub async fn print_markdown(base_dir: &Path, s: &str) -> anyhow::Result<()> { + let env = Environment::for_local_directory(&base_dir)?; let handler = FileResourceHandler::new(200000); - let parser = Parser::new_ext( s, Options::ENABLE_TASKLISTS @@ -29,8 +31,7 @@ pub async fn print_markdown(base_dir: &Path, s: &str) -> anyhow::Result<()> { | Options::ENABLE_GFM, ); - let mut sink = std::io::stderr(); - pulldown_cmark_mdcat::push_tty(&settings, &env, &handler, &mut sink, parser)?; + pulldown_cmark_mdcat::push_tty(&SETTINGS, &env, &handler, &mut std::io::stderr(), parser)?; Ok(()) } diff --git a/crates/omnix-hack/Cargo.toml b/crates/omnix-hack/Cargo.toml new file mode 100644 index 00000000..1f14fab8 --- /dev/null +++ b/crates/omnix-hack/Cargo.toml @@ -0,0 +1,26 @@ +[package] +authors = ["Sridhar Ratnakumar "] +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 } diff --git a/crates/omnix-hack/crate.nix b/crates/omnix-hack/crate.nix new file mode 100644 index 00000000..ce94f5c8 --- /dev/null +++ b/crates/omnix-hack/crate.nix @@ -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 + ; + }; +} diff --git a/crates/omnix-hack/src/config.rs b/crates/omnix-hack/src/config.rs new file mode 100644 index 00000000..f0c81b15 --- /dev/null +++ b/crates/omnix-hack/src/config.rs @@ -0,0 +1,43 @@ +use nix_rs::{ + command::NixCmd, + flake::{command::FlakeOptions, url::FlakeUrl}, +}; +use omnix_common::config::OmConfig; +use serde::Deserialize; + +#[derive(Debug, Deserialize, Clone)] +pub struct HackConfig { + pub cache: CacheConfig, + pub readme: ReadmeConfig, +} + +#[derive(Debug, Deserialize, Clone)] +pub struct CacheConfig { + pub cachix: CachixConfig, +} + +#[derive(Debug, Deserialize, Clone)] +pub struct CachixConfig { + pub enable: bool, + pub name: String, + /// The read-only auth token to use if a private cache + pub auth_token: Option, +} + +#[derive(Debug, Deserialize, Clone)] +pub struct ReadmeConfig(pub String); + +impl HackConfig { + pub async fn from_flake(url: &FlakeUrl) -> anyhow::Result { + let _opts = FlakeOptions { + refresh: true, + ..Default::default() + }; + let v = OmConfig::::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")) + } +} diff --git a/crates/omnix-hack/src/core.rs b/crates/omnix-hack/src/core.rs new file mode 100644 index 00000000..270d942b --- /dev/null +++ b/crates/omnix-hack/src/core.rs @@ -0,0 +1,44 @@ +use std::path::Path; + +use nix_rs::flake::url::FlakeUrl; +use omnix_common::markdown::print_markdown; + +use crate::config::HackConfig; + +// 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 ), 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 . + +"#; + +pub async fn hack_on() -> anyhow::Result<()> { + let here_flake: FlakeUrl = Into::::into(Path::new(".")); + let cfg = HackConfig::from_flake(&here_flake).await?; + + let readme = cfg + .readme + .0 + .replace("OM_SHELL", OM_SHELL) + .replace("OM_IDE", OM_IDE); + + let pwd = std::env::current_dir()?; + + eprintln!(); + print_markdown(&pwd, &readme).await?; + + Ok(()) +} diff --git a/crates/omnix-hack/src/lib.rs b/crates/omnix-hack/src/lib.rs new file mode 100644 index 00000000..61c592e9 --- /dev/null +++ b/crates/omnix-hack/src/lib.rs @@ -0,0 +1,2 @@ +pub mod config; +pub mod core; diff --git a/crates/omnix-init/Cargo.toml b/crates/omnix-init/Cargo.toml index 4f9c89c6..53277380 100644 --- a/crates/omnix-init/Cargo.toml +++ b/crates/omnix-init/Cargo.toml @@ -28,6 +28,5 @@ serde = { workspace = true } serde_json = { workspace = true } thiserror = { workspace = true } tokio = { workspace = true } -termimad = { workspace = true } tracing = { workspace = true } omnix-common = { workspace = true } diff --git a/justfile b/justfile index 33b66d24..3a08f432 100644 --- a/justfile +++ b/justfile @@ -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 diff --git a/nix/modules/om.nix b/nix/modules/om.nix index 7b60fb3a..b0dac101 100644 --- a/nix/modules/om.nix +++ b/nix/modules/om.nix @@ -67,6 +67,35 @@ # 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 inside devShell + + ```sh-session + just watch + ``` + + ## Running through Nix + ```sh-session + nix --accept-flake-config run github:juspay/omnix + ``` + + ## Read more + For details, see [README.md](README.md) + ''; + }; }; }; }