Skip to content

Commit cd7c5ba

Browse files
committed
feature #40 Do not hardcode /nix/store path (Kocal)
This PR was merged into the main branch. Discussion ---------- Do not hardcode `/nix/store` path `@tucksaun` suggested me to not hardcode [`/nix/store`](https://nix.dev/manual/nix/2.24/store/store-path#store-directory) path, since it can be modified IIUC. The command `nix eval --raw --expr builtins.storeDir` gives me the Nix store path: ``` symfony-cli on  nix-no-hardcode via 🐳 orbstack via 🐹 v1.26.4 took 4s ✗ nix eval --raw --expr builtins.storeDir /nix/store% ``` Commits ------- 9f290af Do not hardcode `/nix/store` path
2 parents 3d1da79 + 9f290af commit cd7c5ba

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

discovery_others.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,20 @@ func (s *PHPStore) doDiscover() {
6868
}
6969

7070
// Nix (https://nixos.org/): PHP built with its extensions is stored as
71-
// /nix/store/<hash>-php-with-extensions-<version>. Because /nix/store can
72-
// hold thousands of unrelated packages, glob the matching entries directly
73-
// instead of walking the whole tree.
74-
if dirs, err := filepath.Glob("/nix/store/*-php-with-extensions-*"); err == nil {
75-
for _, dir := range dirs {
76-
// skip .drv files and any other non-directory matches
77-
if fi, err := os.Stat(dir); err != nil || !fi.IsDir() {
78-
continue
71+
// <store>/<hash>-php-with-extensions-<version>. The store directory
72+
// defaults to /nix/store but can be relocated, so ask Nix for its location.
73+
if out, err := exec.Command("nix", "eval", "--raw", "--expr", "builtins.storeDir").Output(); err == nil {
74+
if storeDir := strings.TrimSpace(string(out)); storeDir != "" {
75+
// The store can hold thousands of unrelated packages, so glob the
76+
// matching entries directly instead of walking the whole tree.
77+
dirs, _ := filepath.Glob(filepath.Join(storeDir, "*-php-with-extensions-*"))
78+
for _, dir := range dirs {
79+
// skip .drv files and any other non-directory matches
80+
if fi, err := os.Stat(dir); err != nil || !fi.IsDir() {
81+
continue
82+
}
83+
s.addFromDir(dir, nil, "Nix")
7984
}
80-
s.addFromDir(dir, nil, "Nix")
8185
}
8286
}
8387

0 commit comments

Comments
 (0)