From 80defc2101e7b99a2f379230833676d8feda0343 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Thu, 30 Dec 2021 23:24:56 +0100 Subject: feat(tvix): set up a simple command line parser for nix-store We are going to have a 1:1 drop-in replacement for the old-style nix tools, and this starts implementing the cli parser part. The first step is to have a simple integration test suite that can verify that we match the nix CLI. clap is a super complicated parsing library, but looking through the rest they are either too opinioated to be of use for us, or depend on clap as implementation. Change-Id: I4cf6051f3a4f782c3242fd0d2b9eab3fbe33d8ad Reviewed-on: https://cl.tvl.fyi/c/depot/+/4756 Tested-by: BuildkiteCI Reviewed-by: tazjin Autosubmit: Profpatsch --- tvix/Cargo.lock | 241 ++++++++++++++++++++++++++++++++++++++++++++++ tvix/Cargo.toml | 4 + tvix/src/bin/nix-store.rs | 104 +++++++++++++++++++- 3 files changed, 347 insertions(+), 2 deletions(-) diff --git a/tvix/Cargo.lock b/tvix/Cargo.lock index 75581ea7d9f1..15d97837e6d1 100644 --- a/tvix/Cargo.lock +++ b/tvix/Cargo.lock @@ -2,6 +2,247 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "autocfg" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "clap" +version = "3.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f34b09b9ee8c7c7b400fe2f8df39cafc9538b03d6ba7f4ae13e4cb90bfbb7d" +dependencies = [ + "atty", + "bitflags", + "indexmap", + "os_str_bytes", + "strsim", + "termcolor", + "textwrap", +] + +[[package]] +name = "getrandom" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "hashbrown" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "indexmap" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc633605454125dec4b66843673f01c7df2b89479b32e0ed634e43a91cff62a5" +dependencies = [ + "autocfg", + "hashbrown", +] + +[[package]] +name = "libc" +version = "0.2.112" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125" + +[[package]] +name = "memchr" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" + +[[package]] +name = "os_str_bytes" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64" +dependencies = [ + "memchr", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" + +[[package]] +name = "rand" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7" +dependencies = [ + "rand_core", +] + +[[package]] +name = "redox_syscall" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff" +dependencies = [ + "bitflags", +] + +[[package]] +name = "remove_dir_all" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" +dependencies = [ + "winapi", +] + +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] +name = "tempfile" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22" +dependencies = [ + "cfg-if", + "libc", + "rand", + "redox_syscall", + "remove_dir_all", + "winapi", +] + +[[package]] +name = "termcolor" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "textwrap" +version = "0.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80" + [[package]] name = "tvix" version = "0.1.0" +dependencies = [ + "clap", + "tempfile", +] + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/tvix/Cargo.toml b/tvix/Cargo.toml index 2a59a77eaff5..8b0fbd846a54 100644 --- a/tvix/Cargo.toml +++ b/tvix/Cargo.toml @@ -5,6 +5,10 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[dependencies.clap] +version = "3.0.5" +[dependencies.tempfile] +version = "3.2.0" [[bin]] name = "nix-store" diff --git a/tvix/src/bin/nix-store.rs b/tvix/src/bin/nix-store.rs index 7bf5442cb9ed..72e3bb22048e 100644 --- a/tvix/src/bin/nix-store.rs +++ b/tvix/src/bin/nix-store.rs @@ -1,3 +1,103 @@ -fn main () { - println!("hello, nix"); +fn main() { + main_args(std::env::args().collect()).unwrap_or_else(|e| e.exit()); +} + +pub fn main_args(args: Vec) -> clap::Result { + let matches = clap::App::new("nix-store") + .subcommand(clap::App::new("--add").arg(clap::Arg::new("FILE").required(true).index(1))) + .try_get_matches_from(args.iter())?; + if let Some(add) = matches.subcommand_matches("--add") { + let file = add.value_of("FILE").expect("--add needs a file"); + let file_contents = + std::fs::read_to_string(file).expect(&format!("file {} does not exist", file)); + Ok(NixResult::FileAddedToStore { + content: file_contents, + }) + } else { + panic!("read some arguments that we do not know: {:?}", args) + } +} + +#[derive(Debug, Eq, PartialEq)] +pub enum NixResult { + FileAddedToStore { content: String }, +} + +#[cfg(test)] +mod integration_tests { + use std::{collections::VecDeque, io::Write}; + + use super::*; + + #[derive(Debug)] + enum NixOutput { + Err { + status: i32, + stdout: String, + stderr: String, + }, + Ok { + stdout: String, + stderr: String, + }, + } + + fn run_nix_command(cmd: &str, args: Vec) -> NixOutput { + let out = std::process::Command::new(cmd) + .args(args) + .output() + .expect(&format!("could not run {}", cmd)); + match out.status.code().expect("no status code!") { + 0 => NixOutput::Ok { + stdout: String::from_utf8_lossy(&out.stdout).trim_end().to_string(), + stderr: String::from_utf8_lossy(&out.stderr).trim_end().to_string(), + }, + status => NixOutput::Err { + status, + stdout: String::from_utf8_lossy(&out.stdout).trim_end().to_string(), + stderr: String::from_utf8_lossy(&out.stderr).trim_end().to_string(), + }, + } + } + + fn nix_nix_store<'a>(args: Vec) -> NixResult { + match run_nix_command("nix-store", args) { + err @ NixOutput::Err { .. } => panic!("nix-store --add failed: {:#?}", err), + NixOutput::Ok { stdout, .. } => NixResult::FileAddedToStore { + content: std::fs::read_to_string(&stdout) + .expect(&format!("cannot open {} as store file", stdout)), + }, + } + } + + fn tvix_nix_store<'a>(args: Vec) -> NixResult { + eprintln!("running tvix with arguments {:?}", args); + let mut args = VecDeque::from(args); + args.push_front("tvix-store".to_string()); + super::main_args(Vec::from(args)) + .unwrap_or_else(|e| panic!("clap command line parsing failed:\n{}", e)) + } + + #[test] + fn test_nix_store_add() { + let file_content = "I am a copied file"; + let mut tempfile = tempfile::NamedTempFile::new().expect("cannot create temp file"); + tempfile + .write_all(file_content.as_bytes()) + .expect("could not write to tempfile"); + assert_eq!( + tvix_nix_store(vec![ + "--add".to_string(), + tempfile.path().as_os_str().to_string_lossy().into_owned() + ]), + nix_nix_store(vec![ + "--add".to_string(), + tempfile.path().as_os_str().to_string_lossy().into_owned() + ]), + "added file contents were not the same" + ); + + // make sure the tempfile lives till here + drop(tempfile) + } } -- cgit 1.4.1