@@ -4,21 +4,16 @@ import (
44 "fmt"
55 "os"
66 "os/exec"
7+ "path/filepath"
78 "strings"
89
910 "github.com/urfave/cli/v2"
1011)
1112
12- const enterNix = `
13+ const shellNix = `
1314let
1415 pkgs = import <nixpkgs> {};
15- mkDevShell =
16- if pkgs ? mkDevShell then
17- pkgs.mkDevShell
18- else
19- pkgs.callPackage (fetchTarball
20- "https://github.com/numtide/devshell/archive/master.tar.gz") {}
21- ;
16+ mkDevShell = pkgs.callPackage <devshell> {};
2217in
2318mkDevShell.fromTOML ./devshell.toml
2419`
@@ -27,22 +22,48 @@ var cmdEnter = &cli.Command{
2722 Name : "enter" ,
2823 Aliases : []string {"e" },
2924 Usage : "builds and enters the shell" ,
25+ Flags : []cli.Flag {
26+ & cli.StringFlag {
27+ Name : "path" ,
28+ Usage : "path to the project root" ,
29+ Value : "." ,
30+ },
31+ & cli.StringSliceFlag {
32+ Name : "I" ,
33+ Usage : "Add a path to the NIX_PATH" ,
34+ Value : cli .NewStringSlice ("devshell=https://github.com/numtide/devshell/archive/master.tar.gz" ),
35+ },
36+ },
3037 Action : func (c * cli.Context ) error {
31- // instantiate eval
32- out , err := exec .Command ("nix-instantiate" , "--expr" , enterNix ).Output ()
38+ path := c .String ("path" )
39+ paths := c .StringSlice ("I" )
40+ shellFile := filepath .Join (path , "shell.nix" )
41+
42+ args := []string {"-v" }
43+ exists , err := fileExists (shellFile )
3344 if err != nil {
3445 return err
3546 }
47+ if ! exists {
48+ args = append (args , "--expr" , shellNix )
49+ }
50+ for _ , p := range paths {
51+ args = append (args , "-I" , p )
52+ }
53+
54+ // instantiate eval
55+ out , err := exec .Command ("nix-instantiate" , args ... ).Output ()
56+ if err != nil {
57+ return fmt .Errorf ("nix-instantiate: %w" , err )
58+ }
3659 drvPath := strings .TrimSpace (string (out ))
3760 drvPath = strings .Trim (drvPath , "\" " )
38- fmt .Println ("drvPath" , drvPath )
3961 // realize
4062 out , err = exec .Command ("nix-store" , "--realize" , drvPath ).Output ()
4163 if err != nil {
42- return err
64+ return fmt . Errorf ( "nix-store: %w" , err )
4365 }
4466 outPath := strings .TrimSpace (string (out ))
45- fmt .Println ("outPath" , outPath )
4667 // execute
4768 cmd := exec .Command (outPath , c .Args ().Slice ()... )
4869 cmd .Stdin = os .Stdin
0 commit comments