Skip to content

Commit 44e9050

Browse files
authored
[flakes] add flakes.nix to git only if we are in a git repo (#539)
## Summary This PR looks if the devbox project is in a git repository before adding the flake.nix to a "fake git repository". With this PR, `devbox` with flakes will no longer require `git` as a hard dependency. When I tested with a simple flakes playground project, I observed: 1. if not in a git repository, then flakes.nix can be used by `nix develop`. 2. if in a git repository, then `nix develop` fails if flakes.nix is not tracked. 3. Surprisingly, if in a mercurial repository, then `nix develop` still works if flakes.nix is not tracked by mercurial. Surprising because docs online hint that flakes supports both git and mercurial, but I guess not enforced for this requirement. ## How was it tested? Ran `DEVBOX_FEATURE_FLAKES=1 devbox shell` in devbox projects: 1. in a git repo 2. outside a git repo
1 parent 7810669 commit 44e9050

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

internal/impl/generate.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ func generateForShell(rootPath string, plan *plansdk.ShellPlan, pluginManager *p
3636
}
3737

3838
// Gitignore file is added to the .devbox directory
39-
// TODO savil. Remove this hardcode from here, so this function can be generically defined again
40-
// by accepting the files list parameter.
4139
err := writeFromTemplate(filepath.Join(rootPath, ".devbox"), plan, ".gitignore")
4240
if err != nil {
4341
return errors.WithStack(err)
@@ -112,6 +110,12 @@ func makeFlakeFile(outPath string, plan *plansdk.ShellPlan) error {
112110
return errors.WithStack(err)
113111
}
114112

113+
if !isProjectInGitRepo(outPath) {
114+
// if we are not in a git repository, then carry on
115+
return nil
116+
}
117+
// if we are in a git repository, then nix requires that the flake.nix file be tracked by git
118+
115119
// make an empty git repo
116120
// Alternatively consider: git add intent-to-add path/to/flake.nix, and
117121
// git update-index --assume-unchanged path/to/flake.nix
@@ -136,3 +140,24 @@ func makeFlakeFile(outPath string, plan *plansdk.ShellPlan) error {
136140
}
137141
return nil
138142
}
143+
144+
func isProjectInGitRepo(dir string) bool {
145+
146+
for dir != "/" {
147+
// Look for a .git directory in `dir`
148+
if _, err := os.Stat(filepath.Join(dir, ".git")); err == nil {
149+
// Found a .git
150+
return true
151+
} else if !os.IsNotExist(err) {
152+
// An error means we will not find a git repo so return false
153+
return false
154+
} else {
155+
// No .git directory found, so loop again into the parent dir
156+
dir = filepath.Dir(dir)
157+
continue
158+
}
159+
}
160+
// We reached the fs-root dir, climbed the highest mountain and
161+
// we still haven't found what we're looking for.
162+
return false
163+
}

0 commit comments

Comments
 (0)