Skip to content

Commit aae14a4

Browse files
authored
Improve Nix Sourcing (#543)
## Summary + Don't rely on any binaries (like env) in order to source the nix environment. + Don't pollute the environment with all sorts of variables, only source the three we care about. + Use a well-tested library to parse the env variables ## How was it tested? Removed nix from my path, used ubuntu container.
1 parent 8c9f998 commit aae14a4

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.19
55
require (
66
cuelang.org/go v0.4.3
77
github.com/AlecAivazis/survey/v2 v2.3.6
8+
github.com/MakeNowJust/heredoc/v2 v2.0.1
89
github.com/alessio/shellescape v1.4.1
910
github.com/bmatcuk/doublestar/v4 v4.4.0
1011
github.com/briandowns/spinner v1.19.0
@@ -16,6 +17,7 @@ require (
1617
github.com/getsentry/sentry-go v0.17.0
1718
github.com/google/go-cmp v0.5.9
1819
github.com/google/uuid v1.3.0
20+
github.com/hashicorp/go-envparse v0.1.0
1921
github.com/imdario/mergo v0.3.13
2022
github.com/mattn/go-isatty v0.0.16
2123
github.com/mholt/archiver/v4 v4.0.0-alpha.7
@@ -61,5 +63,4 @@ require (
6163
golang.org/x/sys v0.3.0 // indirect
6264
golang.org/x/term v0.2.0 // indirect
6365
golang.org/x/text v0.5.0 // indirect
64-
gopkg.in/errgo.v2 v2.1.0 // indirect
6566
)

go.sum

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ cuelang.org/go v0.4.3 h1:W3oBBjDTm7+IZfCKZAmC8uDG0eYfJL4Pp/xbbCMKaVo=
22
cuelang.org/go v0.4.3/go.mod h1:7805vR9H+VoBNdWFdI7jyDR3QLUPp4+naHfbcgp55HI=
33
github.com/AlecAivazis/survey/v2 v2.3.6 h1:NvTuVHISgTHEHeBFqt6BHOe4Ny/NwGZr7w+F8S9ziyw=
44
github.com/AlecAivazis/survey/v2 v2.3.6/go.mod h1:4AuI9b7RjAR+G7v9+C4YSlX/YL3K3cWNXgWXOhllqvI=
5+
github.com/MakeNowJust/heredoc/v2 v2.0.1 h1:rlCHh70XXXv7toz95ajQWOWQnN4WNLt0TdpZYIR/J6A=
6+
github.com/MakeNowJust/heredoc/v2 v2.0.1/go.mod h1:6/2Abh5s+hc3g9nbWLe9ObDIOhaRrqsyY9MWy+4JdRM=
57
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s=
68
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w=
79
github.com/alessio/shellescape v1.4.1 h1:V7yhSDDn8LP4lc4jS8pFkt0zCnzVJlG5JXy9BVKJUX0=
@@ -48,6 +50,8 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
4850
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
4951
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
5052
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
53+
github.com/hashicorp/go-envparse v0.1.0 h1:bE++6bhIsNCPLvgDZkYqo3nA+/PFI51pkrHdmPSDFPY=
54+
github.com/hashicorp/go-envparse v0.1.0/go.mod h1:OHheN1GoygLlAkTlXLXvAdnXdZxy8JUweQ1rAXx1xnc=
5155
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog=
5256
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68=
5357
github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk=
@@ -63,9 +67,6 @@ github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgo
6367
github.com/klauspost/pgzip v1.2.5 h1:qnWYvvKqedOF2ulHpMG72XQol4ILEJ8k2wwRl/Km8oE=
6468
github.com/klauspost/pgzip v1.2.5/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
6569
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
66-
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
67-
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
68-
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
6970
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
7071
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
7172
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
@@ -99,8 +100,6 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
99100
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
100101
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
101102
github.com/protocolbuffers/txtpbfmt v0.0.0-20201118171849-f6a6b3f636fc h1:gSVONBi2HWMFXCa9jFdYvYk7IwW/mTLxWOF7rXS4LO0=
102-
github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg=
103-
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
104103
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
105104
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
106105
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
@@ -152,10 +151,7 @@ golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
152151
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
153152
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
154153
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
155-
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
156154
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
157-
gopkg.in/errgo.v2 v2.1.0 h1:0vLT13EuvQ0hNvakwLuFZ/jYrLp5F3kcWHXdRggjCE8=
158-
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
159155
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
160156
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
161157
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

internal/nix/source.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package nix
22

33
import (
4-
"bufio"
54
"bytes"
6-
"fmt"
75
"os"
86
"os/exec"
97
"path/filepath"
108
"strings"
119

10+
"github.com/MakeNowJust/heredoc/v2"
11+
"github.com/hashicorp/go-envparse"
1212
"github.com/pkg/errors"
1313
)
1414

@@ -26,27 +26,41 @@ func SourceNixEnv() error {
2626
)
2727
}
2828

29+
// Source the nix script that sets the environment, and print the environment
30+
// variables that it sets, in a way we can parse.
31+
32+
// NOTE: Only use shell built-ins in this script so that we don't introduce
33+
// any dependencies on an external binary.
34+
script := heredoc.Docf(`
35+
. %s;
36+
echo PATH=$PATH;
37+
echo NIX_PROFILES=$NIX_PROFILES;
38+
echo NIX_SSL_CERT_FILE=$NIX_SSL_CERT_FILE;
39+
echo MANPATH=$MANPATH;
40+
`, srcFile)
2941
cmd := exec.Command(
3042
"/bin/sh",
3143
"-c",
32-
fmt.Sprintf(". %s ; echo '<<<ENVIRONMENT>>>' ; env", srcFile),
44+
script,
3345
)
3446

3547
bs, err := cmd.CombinedOutput()
3648
if err != nil {
37-
return errors.WithStack(err)
49+
// When there's an error, the output is usually an error message that
50+
// was printed to stderr and that we want in the error for debugging.
51+
return errors.Wrap(err, string(bs))
52+
}
53+
envvars, err := envparse.Parse(bytes.NewReader(bs))
54+
55+
if err != nil {
56+
return errors.Wrap(err, "failed to parse nix env vars")
3857
}
39-
s := bufio.NewScanner(bytes.NewReader(bs))
40-
start := false
41-
for s.Scan() {
42-
if s.Text() == "<<<ENVIRONMENT>>>" {
43-
start = true
44-
} else if start {
45-
kv := strings.SplitN(s.Text(), "=", 2)
46-
if len(kv) == 2 {
47-
os.Setenv(kv[0], kv[1])
48-
}
58+
59+
for k, v := range envvars {
60+
if len(strings.TrimSpace(v)) > 0 {
61+
os.Setenv(k, v)
4962
}
5063
}
64+
5165
return nil
5266
}

0 commit comments

Comments
 (0)