From b478c9db2ae22cbf52974b74ec268759023971fb Mon Sep 17 00:00:00 2001 From: "Shahar \"Dawn\" Or" Date: Thu, 28 Mar 2024 21:19:22 +0700 Subject: [PATCH 1/2] feat(flake)!: nix provided via NIX_CMD_PATH Co-authored-by: ruben beck --- .github/workflows/check.yaml | 48 ++++--------------- flake.lock | 89 +++++++++++++++++++++++++++++++++++- flake.nix | 6 ++- src/expression/driver.rs | 8 +--- src/main.rs | 6 +-- src/repl/driver.rs | 8 +--- tests/expression.rs | 9 +++- tests/util/mod.rs | 1 - 8 files changed, 116 insertions(+), 59 deletions(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index 2aaa778..f0053d2 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -11,56 +11,28 @@ jobs: runs-on: ${{ matrix.runner }} steps: - uses: actions/checkout@v4 - - name: cache - uses: FrancisRussell/ferrous-actions@v0.1.0-beta.1 - with: - command: cache - - name: toolchain - uses: FrancisRussell/ferrous-actions@v0.1.0-beta.1 - with: - command: install-rustup - toolchain: stable - - name: install Nix - uses: cachix/install-nix-action@v23 + - uses: DeterminateSystems/nix-installer-action@main + - uses: DeterminateSystems/magic-nix-cache-action@main - name: test - uses: FrancisRussell/ferrous-actions@v0.1.0-beta.1 - with: - command: cargo test + run: nix develop --command cargo test clippy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - name: cache - uses: FrancisRussell/ferrous-actions@v0.1.0-beta.1 - with: - command: cache - - name: toolchain - uses: FrancisRussell/ferrous-actions@v0.1.0-beta.1 - with: - command: install-rustup - toolchain: stable - components: clippy - - name: test - uses: FrancisRussell/ferrous-actions@v0.1.0-beta.1 - with: - command: cargo clippy - args: --all-targets -- --deny warnings + - uses: DeterminateSystems/nix-installer-action@main + - uses: DeterminateSystems/magic-nix-cache-action@main + - name: clippy + run: nix develop --command cargo clippy --all-targets -- --deny warnings fmt: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - name: toolchain - uses: FrancisRussell/ferrous-actions@v0.1.0-beta.1 - with: - command: install-rustup - toolchain: stable + - uses: DeterminateSystems/nix-installer-action@main + - uses: DeterminateSystems/magic-nix-cache-action@main - name: fmt - uses: FrancisRussell/ferrous-actions@v0.1.0-beta.1 - with: - command: cargo fmt - args: --check + run: nix develop --command cargo fmt --check release: if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} diff --git a/flake.lock b/flake.lock index 95c03a3..3076319 100644 --- a/flake.lock +++ b/flake.lock @@ -55,6 +55,22 @@ "url": "https://flakehub.com/f/edolstra/flake-compat/1.tar.gz" } }, + "flake-compat_2": { + "flake": false, + "locked": { + "lastModified": 1673956053, + "narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, "flake-utils": { "inputs": { "systems": "systems" @@ -73,7 +89,77 @@ "type": "github" } }, + "lowdown-src": { + "flake": false, + "locked": { + "lastModified": 1633514407, + "narHash": "sha256-Dw32tiMjdK9t3ETl5fzGrutQTzh2rufgZV4A/BbxuD4=", + "owner": "kristapsdz", + "repo": "lowdown", + "rev": "d2c2b44ff6c27b936ec27358a2653caaef8f73b8", + "type": "github" + }, + "original": { + "owner": "kristapsdz", + "repo": "lowdown", + "type": "github" + } + }, + "nix": { + "inputs": { + "flake-compat": "flake-compat_2", + "lowdown-src": "lowdown-src", + "nixpkgs": "nixpkgs", + "nixpkgs-regression": "nixpkgs-regression" + }, + "locked": { + "lastModified": 1709813453, + "narHash": "sha256-8gNJlBlv2bnffRg0CejiBXc6U/S6YeCLAdHrYvTPyoY=", + "owner": "NixOS", + "repo": "nix", + "rev": "0bd62229cd5c1b615be40ffd5597f76c0cd73828", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "2.18.2", + "repo": "nix", + "type": "github" + } + }, "nixpkgs": { + "locked": { + "lastModified": 1700748986, + "narHash": "sha256-/nqLrNU297h3PCw4QyDpZKZEUHmialJdZW2ceYFobds=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "9ba29e2346bc542e9909d1021e8fd7d4b3f64db0", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-23.05-small", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-regression": { + "locked": { + "lastModified": 1643052045, + "narHash": "sha256-uGJ0VXIhWKGXxkeNnq4TvV3CIOkUJ3PAoLZ3HMzNVMw=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "215d4d0fd80ca5163643b03a33fde804a29cc1e2", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "215d4d0fd80ca5163643b03a33fde804a29cc1e2", + "type": "github" + } + }, + "nixpkgs_2": { "locked": { "lastModified": 1705697961, "narHash": "sha256-XepT3WS516evSFYkme3GrcI3+7uwXHqtHbip+t24J7E=", @@ -95,7 +181,8 @@ "fenix": "fenix", "flake-compat": "flake-compat", "flake-utils": "flake-utils", - "nixpkgs": "nixpkgs" + "nix": "nix", + "nixpkgs": "nixpkgs_2" } }, "rust-analyzer-src": { diff --git a/flake.nix b/flake.nix index 43e93cd..80b0631 100644 --- a/flake.nix +++ b/flake.nix @@ -6,6 +6,7 @@ inputs.fenix.url = "github:nix-community/fenix"; inputs.flake-compat.url = "https://flakehub.com/f/edolstra/flake-compat/1.tar.gz"; inputs.flake-utils.url = "github:numtide/flake-utils"; + inputs.nix.url = "github:NixOS/nix/2.18.2"; outputs = { self, @@ -13,6 +14,7 @@ fenix, flake-compat, flake-utils, + nix, nixpkgs, }: let inherit (nixpkgs.lib) optional; @@ -21,6 +23,7 @@ pkgs = nixpkgs.legacyPackages.${system}; toolchain = fenix.packages.${system}.stable.completeToolchain; craneLib = crane.lib.${system}.overrideToolchain toolchain; + NIX_CMD_PATH = "${nix.packages.${system}.nix}/bin/nix"; commonArgs = { src = craneLib.cleanCargoSource (craneLib.path ./.); @@ -32,7 +35,7 @@ packages.default = craneLib.buildPackage ( commonArgs // { - inherit cargoArtifacts; + inherit cargoArtifacts NIX_CMD_PATH; nativeCheckInputs = [pkgs.nix]; # 1. integration tests execute `nix`, which fails creating `/nix/var` # 2. integration tests require `/dev/ptmx` @@ -41,6 +44,7 @@ ); devShells.default = craneLib.devShell { + inherit NIX_CMD_PATH; inputsFrom = [self.packages.${system}.default]; packages = [ toolchain diff --git a/src/expression/driver.rs b/src/expression/driver.rs index 99f972b..07c0bd2 100644 --- a/src/expression/driver.rs +++ b/src/expression/driver.rs @@ -13,7 +13,6 @@ pub(crate) struct ExpressionDriver { ExampleId, futures::future::LocalBoxFuture<'static, std::io::Result>, )>, - nix_path: camino::Utf8PathBuf, } #[derive(Debug)] @@ -23,16 +22,13 @@ pub(crate) enum ExpressionEvent { } impl ExpressionDriver { - pub(crate) fn new( - nix_path: camino::Utf8PathBuf, - ) -> ( + pub(crate) fn new() -> ( Self, futures::stream::LocalBoxStream<'static, ExpressionEvent>, ) { let (sender, receiver) = futures::channel::mpsc::unbounded(); let driver = Self { sender, - nix_path, nix_processes: Vec::new(), }; (driver, receiver.boxed_local()) @@ -75,7 +71,7 @@ impl ExpressionDriver { } async fn spawn_nix(&mut self, example: ExpressionExample) { - let task = tokio::process::Command::new(&self.nix_path) + let task = tokio::process::Command::new(env!("NIX_CMD_PATH")) .args(["eval", "--expr"]) .arg(example.expression) .output(); diff --git a/src/main.rs b/src/main.rs index bfd613d..5ce1338 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,8 +22,6 @@ use crate::{ #[derive(Debug, clap::Parser)] #[command(version, about)] struct Cli { - /// Path to a `nix` executable - nix_path: camino::Utf8PathBuf, /// pattern (`glob` crate) of markdown filespaths sources: String, } @@ -35,8 +33,8 @@ async fn main() -> anyhow::Result<()> { if examples.is_empty() { anyhow::bail!("could not find any examples"); } - let (repl_driver, repl_events) = ReplDriver::new(cli.nix_path.clone()); - let (expression_driver, expression_events) = ExpressionDriver::new(cli.nix_path); + let (repl_driver, repl_events) = ReplDriver::new(); + let (expression_driver, expression_events) = ExpressionDriver::new(); let (eprintln_driver, eprintln_events) = EprintlnDriver::new(); let inputs = Inputs { diff --git a/src/repl/driver.rs b/src/repl/driver.rs index cd89a94..dab98a4 100644 --- a/src/repl/driver.rs +++ b/src/repl/driver.rs @@ -54,18 +54,14 @@ pub(crate) enum ReplEvent { pub(crate) struct ReplDriver { sessions: std::collections::BTreeMap, sender: futures::channel::mpsc::UnboundedSender, - nix_path: camino::Utf8PathBuf, } impl ReplDriver { - pub(crate) fn new( - nix_path: camino::Utf8PathBuf, - ) -> (Self, futures::stream::LocalBoxStream<'static, ReplEvent>) { + pub(crate) fn new() -> (Self, futures::stream::LocalBoxStream<'static, ReplEvent>) { let (sender, receiver) = futures::channel::mpsc::unbounded::(); let driver = Self { sessions: Default::default(), sender, - nix_path, }; (driver, receiver.boxed_local()) } @@ -140,7 +136,7 @@ impl ReplDriver { } }; - let child = pty_process::Command::new(&self.nix_path) + let child = pty_process::Command::new(env!("NIX_CMD_PATH")) .args(["repl", "--quiet"]) .spawn(&pts); diff --git a/tests/expression.rs b/tests/expression.rs index 64c5519..bd8f512 100644 --- a/tests/expression.rs +++ b/tests/expression.rs @@ -1,5 +1,7 @@ mod util; +use std::os::unix::fs::PermissionsExt; + use assert_fs::fixture::FileWriteStr; use indoc::indoc; use predicates::{ @@ -59,14 +61,17 @@ fn io_error() { "}) .unwrap(); + let mut perms = file.metadata().unwrap().permissions(); + perms.set_mode(0o000); + std::fs::set_permissions(&file, perms).unwrap(); + let mut eelco = assert_cmd::Command::cargo_bin("eelco").unwrap(); - eelco.arg("brix"); eelco.arg(file.as_os_str()); eelco .assert() .failure() - .stderr("Error: No such file or directory (os error 2)\n"); + .stderr(predicates::str::starts_with("Error: ")); }); } diff --git a/tests/util/mod.rs b/tests/util/mod.rs index 3940eca..98fb342 100644 --- a/tests/util/mod.rs +++ b/tests/util/mod.rs @@ -4,7 +4,6 @@ pub fn with_eelco(f: impl FnOnce(&mut NamedTempFile, &mut assert_cmd::Command)) let mut tmpfile = NamedTempFile::new("we-dont-particularly-mind.md").unwrap(); let mut command = assert_cmd::Command::cargo_bin("eelco").unwrap(); - command.arg("nix"); command.arg(tmpfile.as_os_str()); f(&mut tmpfile, &mut command); drop(tmpfile); From 307e422a797d3f8cd8b35e091ec6f7577c59ecb6 Mon Sep 17 00:00:00 2001 From: "Shahar \"Dawn\" Or" Date: Sat, 30 Mar 2024 21:07:47 +0700 Subject: [PATCH 2/2] chore(deps): set nix input to latest-release branch Co-authored-by: ruben beck Co-authored-by: Ishwar --- flake.lock | 56 ++++++++++++++----------------------- flake.nix | 3 +- src/app/state.rs | 18 ++---------- src/app/state/repl_state.rs | 3 -- 4 files changed, 25 insertions(+), 55 deletions(-) diff --git a/flake.lock b/flake.lock index 3076319..978b7de 100644 --- a/flake.lock +++ b/flake.lock @@ -89,56 +89,58 @@ "type": "github" } }, - "lowdown-src": { + "libgit2": { "flake": false, "locked": { - "lastModified": 1633514407, - "narHash": "sha256-Dw32tiMjdK9t3ETl5fzGrutQTzh2rufgZV4A/BbxuD4=", - "owner": "kristapsdz", - "repo": "lowdown", - "rev": "d2c2b44ff6c27b936ec27358a2653caaef8f73b8", + "lastModified": 1711487526, + "narHash": "sha256-UzWAyEd171lA5XtwDpo77Ahy/2yduATyoZrwD0UQBJ0=", + "owner": "libgit2", + "repo": "libgit2", + "rev": "4bb051adec18a10b7efbb4a612a83c1c1b8d38a0", "type": "github" }, "original": { - "owner": "kristapsdz", - "repo": "lowdown", + "owner": "libgit2", + "repo": "libgit2", "type": "github" } }, "nix": { "inputs": { "flake-compat": "flake-compat_2", - "lowdown-src": "lowdown-src", - "nixpkgs": "nixpkgs", + "libgit2": "libgit2", + "nixpkgs": [ + "nixpkgs" + ], "nixpkgs-regression": "nixpkgs-regression" }, "locked": { - "lastModified": 1709813453, - "narHash": "sha256-8gNJlBlv2bnffRg0CejiBXc6U/S6YeCLAdHrYvTPyoY=", + "lastModified": 1711461841, + "narHash": "sha256-iRtvOcJbohyhav+deEajI/Ln/LU/6WqSfLyXDQaNEro=", "owner": "NixOS", "repo": "nix", - "rev": "0bd62229cd5c1b615be40ffd5597f76c0cd73828", + "rev": "3272ed0d5813175ea240ce2dfadf9de24c9d14ae", "type": "github" }, "original": { "owner": "NixOS", - "ref": "2.18.2", + "ref": "latest-release", "repo": "nix", "type": "github" } }, "nixpkgs": { "locked": { - "lastModified": 1700748986, - "narHash": "sha256-/nqLrNU297h3PCw4QyDpZKZEUHmialJdZW2ceYFobds=", + "lastModified": 1705697961, + "narHash": "sha256-XepT3WS516evSFYkme3GrcI3+7uwXHqtHbip+t24J7E=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "9ba29e2346bc542e9909d1021e8fd7d4b3f64db0", + "rev": "e5d1c87f5813afde2dda384ac807c57a105721cc", "type": "github" }, "original": { "owner": "NixOS", - "ref": "nixos-23.05-small", + "ref": "nixpkgs-unstable", "repo": "nixpkgs", "type": "github" } @@ -159,22 +161,6 @@ "type": "github" } }, - "nixpkgs_2": { - "locked": { - "lastModified": 1705697961, - "narHash": "sha256-XepT3WS516evSFYkme3GrcI3+7uwXHqtHbip+t24J7E=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "e5d1c87f5813afde2dda384ac807c57a105721cc", - "type": "github" - }, - "original": { - "owner": "NixOS", - "ref": "nixpkgs-unstable", - "repo": "nixpkgs", - "type": "github" - } - }, "root": { "inputs": { "crane": "crane", @@ -182,7 +168,7 @@ "flake-compat": "flake-compat", "flake-utils": "flake-utils", "nix": "nix", - "nixpkgs": "nixpkgs_2" + "nixpkgs": "nixpkgs" } }, "rust-analyzer-src": { diff --git a/flake.nix b/flake.nix index 80b0631..9577804 100644 --- a/flake.nix +++ b/flake.nix @@ -6,7 +6,8 @@ inputs.fenix.url = "github:nix-community/fenix"; inputs.flake-compat.url = "https://flakehub.com/f/edolstra/flake-compat/1.tar.gz"; inputs.flake-utils.url = "github:numtide/flake-utils"; - inputs.nix.url = "github:NixOS/nix/2.18.2"; + inputs.nix.inputs.nixpkgs.follows = "nixpkgs"; + inputs.nix.url = "github:NixOS/nix/latest-release"; outputs = { self, diff --git a/src/app/state.rs b/src/app/state.rs index c645dc5..80fa24f 100644 --- a/src/app/state.rs +++ b/src/app/state.rs @@ -136,7 +136,7 @@ impl State { acc.push(ch.into()); let string = String::from_utf8(strip_ansi_escapes::strip(acc)?)?; - if string == "nix-repl> " { + if string.ends_with("nix-repl> ") { session_live.expecting = ReplSessionExpecting::Nothing; self.next_query(&id)? } else { @@ -158,7 +158,7 @@ impl State { expected_result: expected_result.clone(), } } else { - ReplSessionExpecting::BlankLine { saw_cr: false } + ReplSessionExpecting::Prompt(String::new()) }; vec![] } else { @@ -188,20 +188,6 @@ impl State { session_live.expecting = ReplSessionExpecting::Prompt(String::new()); vec![] } - ReplSessionExpecting::BlankLine { saw_cr: false } => { - anyhow::ensure!( - ch == b'\r', - "expecting carriage return, got {:?}", - ch as char, - ); - session_live.expecting = ReplSessionExpecting::BlankLine { saw_cr: true }; - vec![] - } - ReplSessionExpecting::BlankLine { saw_cr: true } => { - anyhow::ensure!(ch == b'\n', "expecting line feed, got {:?}", ch as char,); - session_live.expecting = ReplSessionExpecting::Prompt(String::new()); - vec![] - } }; Ok(output) diff --git a/src/app/state/repl_state.rs b/src/app/state/repl_state.rs index 5209f97..4b23410 100644 --- a/src/app/state/repl_state.rs +++ b/src/app/state/repl_state.rs @@ -55,9 +55,6 @@ pub(crate) enum ReplSessionExpecting { acc: String, expected_result: ExpectedResult, }, - BlankLine { - saw_cr: bool, - }, } impl ReplSessionLive {