Skip to content

Commit 50fd63b

Browse files
authored
internal/shellgen: stop adding flake to git (#1822)
In b390352 the call to nix print-dev-env was updated to explicitly use a path flake reference. This means that Nix no longer tries to build the flake as part of a git repo, and therefore we no longer need to add it to git.
1 parent d3c064a commit 50fd63b

File tree

1 file changed

+4
-63
lines changed

1 file changed

+4
-63
lines changed

internal/shellgen/generate.go

Lines changed: 4 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"embed"
1111
"io/fs"
1212
"os"
13-
"os/exec"
1413
"path/filepath"
1514
"runtime/trace"
1615
"strings"
@@ -60,9 +59,8 @@ func GenerateForPrintEnv(ctx context.Context, devbox devboxer) error {
6059
return redact.Errorf("write glibc patch flake to directory: %v", err)
6160
}
6261
}
63-
err = makeFlakeFile(devbox, outPath, plan)
64-
if err != nil {
65-
return errors.WithStack(err)
62+
if err := makeFlakeFile(devbox, plan); err != nil {
63+
return err
6664
}
6765

6866
return WriteScriptsToFiles(devbox)
@@ -184,68 +182,11 @@ var templateFuncs = template.FuncMap{
184182
"debug": debug.IsEnabled,
185183
}
186184

187-
func makeFlakeFile(d devboxer, outPath string, plan *flakePlan) error {
185+
func makeFlakeFile(d devboxer, plan *flakePlan) error {
188186
flakeDir := FlakePath(d)
189187
templateName := "flake.nix"
190188
if featureflag.RemoveNixpkgs.Enabled() {
191189
templateName = "flake_remove_nixpkgs.nix"
192190
}
193-
err := writeFromTemplate(flakeDir, plan, templateName, "flake.nix")
194-
if err != nil {
195-
return errors.WithStack(err)
196-
}
197-
198-
if !isProjectInGitRepo(outPath) {
199-
// if we are not in a git repository, then carry on
200-
return nil
201-
}
202-
// if we are in a git repository, then nix requires that the flake.nix file be tracked by git
203-
204-
// make an empty git repo
205-
// Alternatively consider: git add intent-to-add path/to/flake.nix, and
206-
// git update-index --assume-unchanged path/to/flake.nix
207-
// https://nixos.wiki/wiki/Flakes#How_to_add_a_file_locally_in_git_but_not_include_it_in_commits
208-
cmd := exec.Command("git", "-C", flakeDir, "init")
209-
if debug.IsEnabled() {
210-
cmd.Stdin = os.Stdin
211-
cmd.Stdout = os.Stdout
212-
cmd.Stderr = os.Stderr
213-
}
214-
err = cmd.Run()
215-
if err != nil {
216-
return errors.WithStack(err)
217-
}
218-
219-
// Any files that flake.nix needs at build time must be in git.
220-
// Otherwise, Nix won't copy it into the flake's build environment.
221-
cmd = exec.Command("git", "-C", flakeDir, "add", "flake.nix")
222-
if plan.needsGlibcPatch() {
223-
cmd.Args = append(cmd.Args, "glibc-patch/flake.nix")
224-
}
225-
if debug.IsEnabled() {
226-
cmd.Stdin = os.Stdin
227-
cmd.Stdout = os.Stdout
228-
cmd.Stderr = os.Stderr
229-
}
230-
return errors.WithStack(cmd.Run())
231-
}
232-
233-
func isProjectInGitRepo(dir string) bool {
234-
for dir != "/" {
235-
// Look for a .git directory in `dir`
236-
_, err := os.Stat(filepath.Join(dir, ".git"))
237-
if err == nil {
238-
// Found a .git
239-
return true
240-
}
241-
if !errors.Is(err, fs.ErrNotExist) {
242-
// An error means we will not find a git repo so return false
243-
return false
244-
}
245-
// No .git directory found, so loop again into the parent dir
246-
dir = filepath.Dir(dir)
247-
}
248-
// We reached the fs-root dir, climbed the highest mountain and
249-
// we still haven't found what we're looking for.
250-
return false
191+
return writeFromTemplate(flakeDir, plan, templateName, "flake.nix")
251192
}

0 commit comments

Comments
 (0)