Skip to content

Commit b847291

Browse files
committed
devshell enter: use shell.nix if it exists
Allow to fallback on shell.nix to allow user overrides.
1 parent 964058f commit b847291

2 files changed

Lines changed: 50 additions & 13 deletions

File tree

go/devshell/cmd_enter.go

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = `
1314
let
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> {};
2217
in
2318
mkDevShell.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

go/devshell/path_utils.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import (
4+
"os"
5+
)
6+
7+
func fileExists(path string) (bool, error) {
8+
_, err := os.Stat(path)
9+
if err == nil {
10+
return true, nil
11+
}
12+
if os.IsNotExist(err) {
13+
return false, nil
14+
}
15+
return false, err
16+
}

0 commit comments

Comments
 (0)