Skip to content

Commit

Permalink
feat(flake)!: nix provided via NIX_CMD_PATH
Browse files Browse the repository at this point in the history
Co-authored-by: ruben beck <[email protected]>
  • Loading branch information
mightyiam and ubbabeck committed Apr 11, 2024
1 parent 10d8664 commit b478c9d
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 59 deletions.
48 changes: 10 additions & 38 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,28 @@ jobs:
runs-on: ${{ matrix.runner }}
steps:
- uses: actions/checkout@v4
- name: cache
uses: FrancisRussell/[email protected]
with:
command: cache
- name: toolchain
uses: FrancisRussell/[email protected]
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/[email protected]
with:
command: cargo test
run: nix develop --command cargo test

clippy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: cache
uses: FrancisRussell/[email protected]
with:
command: cache
- name: toolchain
uses: FrancisRussell/[email protected]
with:
command: install-rustup
toolchain: stable
components: clippy
- name: test
uses: FrancisRussell/[email protected]
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/[email protected]
with:
command: install-rustup
toolchain: stable
- uses: DeterminateSystems/nix-installer-action@main
- uses: DeterminateSystems/magic-nix-cache-action@main
- name: fmt
uses: FrancisRussell/[email protected]
with:
command: cargo fmt
args: --check
run: nix develop --command cargo fmt --check

release:
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
Expand Down
89 changes: 88 additions & 1 deletion flake.lock

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

6 changes: 5 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
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,
crane,
fenix,
flake-compat,
flake-utils,
nix,
nixpkgs,
}: let
inherit (nixpkgs.lib) optional;
Expand All @@ -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 ./.);
Expand All @@ -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`
Expand All @@ -41,6 +44,7 @@
);

devShells.default = craneLib.devShell {
inherit NIX_CMD_PATH;
inputsFrom = [self.packages.${system}.default];
packages = [
toolchain
Expand Down
8 changes: 2 additions & 6 deletions src/expression/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub(crate) struct ExpressionDriver {
ExampleId,
futures::future::LocalBoxFuture<'static, std::io::Result<std::process::Output>>,
)>,
nix_path: camino::Utf8PathBuf,
}

#[derive(Debug)]
Expand All @@ -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())
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 2 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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 {
Expand Down
8 changes: 2 additions & 6 deletions src/repl/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,14 @@ pub(crate) enum ReplEvent {
pub(crate) struct ReplDriver {
sessions: std::collections::BTreeMap<ExampleId, (pty_process::Pty, tokio::process::Child)>,
sender: futures::channel::mpsc::UnboundedSender<ReplEvent>,
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::<ReplEvent>();
let driver = Self {
sessions: Default::default(),
sender,
nix_path,
};
(driver, receiver.boxed_local())
}
Expand Down Expand Up @@ -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);

Expand Down
9 changes: 7 additions & 2 deletions tests/expression.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod util;

use std::os::unix::fs::PermissionsExt;

use assert_fs::fixture::FileWriteStr;
use indoc::indoc;
use predicates::{
Expand Down Expand Up @@ -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: "));
});
}
1 change: 0 additions & 1 deletion tests/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b478c9d

Please sign in to comment.