Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The entire interface can be summarized by the following excerpt of the manpage:
ns [OPTION]… [SOURCE]… [TARGET_DIR]
```

> <pre>
> Each SOURCE is positional and may be one of:
>
> URI#ATTR select a devshell or package from a flake
Expand All @@ -23,6 +24,7 @@ ns [OPTION]… [SOURCE]… [TARGET_DIR]
>
> If a single SOURCE is provided and it is a directory, ns switches into
> it by default unless a TARGET_DIR is explicitly given.
> </pre>

Try it from anywhere:

Expand Down
4 changes: 2 additions & 2 deletions bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ open Ns
open Ns.Cmd
open Ns.Util

let main ({ installables; target_info } : Cli.strategy) =
let main ({ installables; target_info; printcmd } : Cli.strategy) =
let ({ entrypoint; attribute; subshell_dir } : Cli.target_info) = target_info in
Option.value ~default:(Option.value ~default:(Sys.getcwd ()) entrypoint) subshell_dir
|> Unix.cd;
Expand All @@ -26,7 +26,7 @@ let main ({ installables; target_info } : Cli.strategy) =
|>+ [ "--command"; Unix.shell ]
| None -> Cmd.builder "nix" |>+ [ "shell" ] |>+ installables
in
ignore (Cmd.run cmd)
if printcmd then print_endline (Cmd.to_string cmd) else ignore (Cmd.run cmd)
;;

Cli.eval main
2 changes: 1 addition & 1 deletion dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

(name ns)

(version 0.4.0)
(version 0.4.1)

(generate_opam_files true)

Expand Down
23 changes: 21 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
doCheck = true;
nativeCheckInputs = attrValues {
inherit (pkgs) ocamlformat;
inherit (pkgs) rustc clippy rustfmt;
};
checkPhase = ''
red() {
Expand All @@ -50,9 +51,24 @@
printf "$(red "Error:") Generated opam file does not match provided opam file\n"
exit 1
fi
if ! dune test --force --no-buffer --display=quiet; then
printf "\n"
printf "$(red "Error:") dune test reported test failures\n"
exit 1
fi
if ! clippy-driver --test "${finalAttrs.src}/test/test_ns.rs" -Dwarnings; then
printf "\n"
printf "$(red "Error:") test.rs contains warnings\n"
exit 1
fi
if ! dune fmt --diff-command="diff --color=always"; then
printf "\n"
printf "$(red "Error:") Some files are not properly formatted\n"
printf "$(red "Error:") Some OCaml files are not properly formatted\n"
exit 1
fi
if ! rustfmt --check --color=always "${finalAttrs.src}/test/test_ns.rs"; then
printf "\n"
printf "$(red "Error:") test.rs is not properly formatted\n"
exit 1
fi
'';
Expand All @@ -78,14 +94,17 @@
default = pkgs.mkShell {
inputsFrom = [ self.packages.${pkgs.stdenv.hostPlatform.system}.default ];
packages = attrValues {
inherit (pkgs) ocamlformat nil;
inherit (pkgs) ocamlformat nil rustc clippy rustfmt;
inherit (pkgs.ocamlPackages) ocaml-lsp odoc;
};
shellHook = ''
dune build # Ensure build artifacts exist for LSP
'';
};
});
checks = eachSystem (pkgs: {
inherit (self.packages.${pkgs.stdenv.hostPlatform.system}) default;
});
formatter = eachSystem (pkgs: pkgs.nixpkgs-fmt);
};
}
14 changes: 11 additions & 3 deletions lib/cli.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ module Cli = struct
type strategy =
{ installables : string list
; target_info : target_info
; printcmd : bool
}

(** Positional URI arguments passed to the program via the command line. *)
let uris = Arg.(value & pos_all dirpath [] & info [])

let printcmd =
let doc = "Print the command to stdout instead of executing it." in
Arg.(value & flag & info [ "printcmd" ] ~doc)
;;

(** Processes the args so that the main function knows what to do with them. *)
let make_strategy =
let build original_args =
let build original_args printcmd =
let installables, target_info =
let default_installables = []
and default_target =
Expand Down Expand Up @@ -81,9 +87,9 @@ module Cli = struct
, { entrypoint = None; attribute = None; subshell_dir = Some subshell_dir } )
| _ -> Uri.parse_targets_tr original_args, default_target)
in
{ installables; target_info }
{ installables; target_info; printcmd }
in
Term.(const build $ uris)
Term.(const build $ uris $ printcmd)
;;

let cmd entrypoint =
Expand All @@ -104,6 +110,8 @@ module Cli = struct
; `P
"If a single SOURCE is provided and it is a directory, ns switches into it by \
default unless a TARGET_DIR is explicitly given."
; `S Manpage.s_options
; `S ""
; `S Manpage.s_common_options
; `S ""
; `S Manpage.s_examples
Expand Down
3 changes: 3 additions & 0 deletions lib/cmd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ module Cmd = struct
| args -> List.fold_left (fun cmd arg -> Cmd.add_arg cmd arg) cmd args
;;

(** Convert a command into a string *)
let to_string cmd = Cmd.to_string cmd

(** Run a command

Example:
Expand Down
2 changes: 1 addition & 1 deletion ns.opam
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
version: "0.4.0"
version: "0.4.1"
synopsis: "A unified interface for nix shell."
description:
"An intuitive nix shell interface that unifies the `nix shell`, `nix develop` and `nix-shell` commands."
Expand Down
14 changes: 14 additions & 0 deletions test/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
;; Define the test stanza for dune test

(test
(name test_ns)
(deps test_ns src/flake/flake.nix src/shell/shell.nix)
(flags (:standard)))

;; Rule to compile the Rust test file into a binary

(rule
(targets test_ns) ; the Dune target name
(deps test_ns.rs) ; path relative to this dune file
(action
(run rustc --test %{deps} -o %{targets} --allow warnings)))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pretty cool that you can do this

12 changes: 12 additions & 0 deletions test/src/flake/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
outputs = { self, nixpkgs }:
let
inherit (nixpkgs.lib) genAttrs;
inherit (nixpkgs.lib.systems) flakeExposed;
eachSystem = f: genAttrs flakeExposed (system: f (import nixpkgs { inherit system; }));
in
{
packages = eachSystem (pkgs: { default = pkgs.hello; });
};
}
6 changes: 6 additions & 0 deletions test/src/shell/shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{ pkgs ? import <nixpkgs> { } }:
pkgs.mkShell {
buildInputs = builtins.attrValues {
inherit (pkgs) hello;
};
}
10 changes: 10 additions & 0 deletions test/test_ns.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
let color_mode =
Option.value
~default:"auto"
(Option.map
(fun force -> if force = "1" then "always" else "never")
(Sys.getenv_opt "CLICOLOR_FORCE"))
;;

let cmd = Printf.sprintf {| sh -c ./test_ns --color=%s --no-capture |} color_mode in
exit (Sys.command cmd)
Loading