@@ -10,7 +10,6 @@ import (
10
10
"embed"
11
11
"io/fs"
12
12
"os"
13
- "os/exec"
14
13
"path/filepath"
15
14
"runtime/trace"
16
15
"strings"
@@ -60,9 +59,8 @@ func GenerateForPrintEnv(ctx context.Context, devbox devboxer) error {
60
59
return redact .Errorf ("write glibc patch flake to directory: %v" , err )
61
60
}
62
61
}
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
66
64
}
67
65
68
66
return WriteScriptsToFiles (devbox )
@@ -184,68 +182,11 @@ var templateFuncs = template.FuncMap{
184
182
"debug" : debug .IsEnabled ,
185
183
}
186
184
187
- func makeFlakeFile (d devboxer , outPath string , plan * flakePlan ) error {
185
+ func makeFlakeFile (d devboxer , plan * flakePlan ) error {
188
186
flakeDir := FlakePath (d )
189
187
templateName := "flake.nix"
190
188
if featureflag .RemoveNixpkgs .Enabled () {
191
189
templateName = "flake_remove_nixpkgs.nix"
192
190
}
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" )
251
192
}
0 commit comments