Skip to content

Commit 775e896

Browse files
pyrex41Reuben Brooksclaude
authored
fix: detect Nix install path for fish and tcsh shell aliases (#921)
When Granted is installed via Nix, the shell scripts (assume.fish, assume.tcsh) are placed in the share/ directory rather than bin/. This causes the shell alias setup to generate incorrect paths for fish and tcsh users. This commit adds detection for Nix installations by checking if the granted binary is located in /nix/store/. When detected, the alias is generated with the correct path to the share/ directory. Fixes #920 Co-authored-by: Reuben Brooks <reuben.brooks@facilitygrid.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 5fb666f commit 775e896

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

pkg/alias/alias.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"fmt"
1212
"os"
1313
"os/exec"
14+
"path/filepath"
1415
"strings"
1516

1617
"github.com/AlecAivazis/survey/v2"
@@ -41,6 +42,36 @@ const devFishAlias = `alias dassume="source /usr/local/bin/dassume.fish"`
4142
const devTcshAlias = `alias dassume 'source /usr/local/bin/dassume.tcsh'`
4243
const devDefaultAlias = `alias dassume=". dassume"`
4344

45+
// getNixSharePath returns the path to the Nix share directory if granted
46+
// is installed via Nix, otherwise returns an empty string.
47+
// Nix installs assume.fish to <store-path>/share/assume.fish rather than
48+
// <store-path>/bin/assume.fish
49+
func getNixSharePath() string {
50+
execPath, err := os.Executable()
51+
if err != nil {
52+
return ""
53+
}
54+
55+
// Resolve symlinks to get the real path
56+
realPath, err := filepath.EvalSymlinks(execPath)
57+
if err != nil {
58+
return ""
59+
}
60+
61+
// Check if we're in a Nix store path (e.g., /nix/store/xxx-granted-x.x.x/bin/granted)
62+
if !strings.HasPrefix(realPath, "/nix/store/") {
63+
return ""
64+
}
65+
66+
// The Nix package structure is: /nix/store/<hash>-granted-<version>/bin/granted
67+
// We need: /nix/store/<hash>-granted-<version>/share/
68+
binDir := filepath.Dir(realPath)
69+
storeDir := filepath.Dir(binDir)
70+
shareDir := filepath.Join(storeDir, "share")
71+
72+
return shareDir
73+
}
74+
4475
func GetDefaultAlias() string {
4576
if build.IsDev() {
4677
return devDefaultAlias
@@ -52,6 +83,11 @@ func GetFishAlias() string {
5283
return devFishAlias
5384
}
5485

86+
// Check if we're installed via Nix - Nix puts assume.fish in share/ not bin/
87+
if nixShare := getNixSharePath(); nixShare != "" {
88+
return fmt.Sprintf(`alias assume="source %s/assume.fish"`, nixShare)
89+
}
90+
5591
// if 'brew' exists on the path, use the brew prefix rather than /usr/local/bin when installing the alias.
5692
// chrnorm: there's not really a better way to determine if we've been installed with brew or not.
5793
if _, err := exec.LookPath("brew"); err == nil {
@@ -65,6 +101,11 @@ func GetTcshAlias() string {
65101
return devTcshAlias
66102
}
67103

104+
// Check if we're installed via Nix - Nix puts assume.tcsh in share/ not bin/
105+
if nixShare := getNixSharePath(); nixShare != "" {
106+
return fmt.Sprintf(`alias assume 'source %s/assume.tcsh'`, nixShare)
107+
}
108+
68109
// if 'brew' exists on the path, use the brew prefix rather than /usr/local/bin when installing the alias.
69110
// chrnorm: there's not really a better way to determine if we've been installed with brew or not.
70111
if _, err := exec.LookPath("brew"); err == nil {

0 commit comments

Comments
 (0)