Hi @jacob,
Thank you for letting us know - we’re working hard to improve the process and your feedback is absolutely essential for making that happen!
Latest & greatest
First thing first, the results of your flox init
invocation suggests you may be using an old version of flox? Can you append the output of flox --version
?
In general I’d suggest following the upgrade instructions often to stay up with the latest revision, but you may find it more convenient to use the following trick to install the latest flox with flox.
flox pull -e flox/default # and then accept the default prompts
flox activate -e flox/default
flox --version # as of now should return 0.0.7-r93
Thanks for providing a pointer to your source - it makes it so much easier to work through an example when we can work with the same project. I made a fork of your package to github:flox-examples/cloudflare-go and you can cherry-pick commit a5bb93b to replicate everything you see in this append.
Note that all of the steps in this posting are listed in the git log for the above commit.
Building your package(s)
I can almost hear you say “but I only want a developer environment”, and while that is certainly understandable the thing to keep in mind with flox is that the developer environment is comprised of a build environment with layers of extra tooling thrown in on top, so it’s easier to start with a successful build and add on from there.
cloudflare-go
It’s customary to name the flox/Nix package after the repository itself, and the following will create the flox expression for this package:
-
invoke flox init
- select
buildGoModule()
template, accept defaults
-
edit pkgs/cloudflare-go/default.nix
- set version to
"0.57.1"
manually (this will be automatically inferred from idiomatic conventions in future)
- then update version to
"0.57.1-${getRev src}"
if you want the getRev()
function to automatically append revision count to version number (this will be included in the default template very soon)
- uncomment
vendorSha256 = lib.fakeSha256
line and run flox build
to download and identify checksum of package containing vendored dependencies, then set vendorSha256
accordingly (this will be automated in future with the new flox tidy
command)
- add the line:
subPackages = [ "." "cmd/flarectl" ];
That last line (2.4) is necessary because the build was tripping over a problem building the internal/tools
subPackage, and setting this allowed Nix to skip that part. This by far was the hardest part of this exercise, requiring some Nix spelunking skills.
cloudflare-go-internal-tools
As mentioned above the build of the main package was not able to build the internal/tools
subpackage, and I couldn’t figure out how to fix that (sorry I’m not a go developer!), so I instead created a second flox expression within your repository to build that subPackage if you need it. (If you don’t need it, or if you can get it building from within the main package then we can delete this.)
-
run flox init
again
- select
buildGoModule()
template, change package name in prompt to cloudflare-go-internal-tools
-
edit pkgs/cloudflare-go-internal-tools/default.nix
- perform same updates as 2.1, 2.2, 2.3 above
- update the definition of
src
to be self + "/internal/tools"
Building your package(s)
Now that you have flox expressions for your packages, building should be as simple as running flox build
, but sadly:
[brantley@clubsoda:~/src/cloudflare-go]$ flox build
Select package for flox build
> cloudflare-go
cloudflare-go-internal-tools
HINT: avoid selecting a package next time with:
$ flox build -A cloudflare-go
error: builder for '/nix/store/yffhqcm20gcxmvh3nmsbybpl7w4c2rgx-cloudflare-go-0.57.1-dirty.drv' failed with exit code 1;
last 10 log lines:
> --- PASS: TestUpdateZoneSSLSettings (0.00s)
> === RUN TestError_CreateErrors
> --- PASS: TestError_CreateErrors (0.00s)
> === RUN ExampleDuration
> --- PASS: ExampleDuration (0.00s)
> === RUN ExampleLogpushJob_MarshalJSON
> --- PASS: ExampleLogpushJob_MarshalJSON (0.00s)
> FAIL
> FAIL github.com/cloudflare/cloudflare-go 3.269s
> FAIL
For full logs, run 'nix log /nix/store/yffhqcm20gcxmvh3nmsbybpl7w4c2rgx-cloudflare-go-0.57.1-dirty.drv'.
Oh no! Looks like the package built but the unit tests did not pass. That last line provides a helpful hint for accessing the failed build logs, and looking through that we can get more detail of what failed:
[brantley@clubsoda:~/src/cloudflare-go]$ flox nix log /nix/store/yffhqcm20gcxmvh3nmsbybpl7w4c2rgx-cloudflare-go-0.57.1-dirty.drv | grep -i fail
HTTP request failed: Get "https://developers.cloudflare.com/ssl/static/origin_ca_ecc_root.pem": dial tcp: lookup developers.cloudflare.com on [::1]:53: read udp [::1]:45278->[::1]:53: read: connection refused
HTTP request failed: Get "https://developers.cloudflare.com/ssl/static/origin_ca_rsa_root.pem": dial tcp: lookup developers.cloudflare.com on [::1]:53: read udp [::1]:49159->[::1]:53: read: connection refused
--- FAIL: TestOriginCA_OriginCARootCertificate (0.00s)
=== RUN TestListZonesFailingPages
--- PASS: TestListZonesFailingPages (0.03s)
FAIL
FAIL github.com/cloudflare/cloudflare-go 3.269s
FAIL
This makes complete sense - the Nix build sandbox is completely isolated from the network so that test cannot succeed. We’ll simply need to disable that particular test in the Nix build.
Disabling tests in the Nix sandbox
Again a disclaimer: I am most definitely not a go developer so this is probably not the best way to do it, but the following patch was able to disable the one TestOriginCA_OriginCARootCertificate
test and allow the build to succeed:
diff --git a/origin_ca_test.go b/origin_ca_test.go
index 0051080..b524906 100644
--- a/origin_ca_test.go
+++ b/origin_ca_test.go
@@ -6,6 +6,7 @@ import (
"encoding/pem"
"fmt"
"net/http"
+ "os"
"testing"
"time"
@@ -224,7 +225,14 @@ func TestOriginCA_RevokeCertificate(t *testing.T) {
}
}
+func skipNix(t *testing.T) {
+ if os.Getenv("NIX_BUILD_TOP") != "" {
+ t.Skip("Skipping testing in Nix sandbox")
+ }
+}
+
func TestOriginCA_OriginCARootCertificate(t *testing.T) {
+ skipNix(t)
setup()
defer teardown()
… and then following the addition of that patch the build succeeds and the package seems to work:
[brantley@clubsoda:~/src/cloudflare-go]$ flox build -A cloudflare-go
[brantley@clubsoda:~/src/cloudflare-go]$ result/bin/flarectl
NAME:
flarectl - Cloudflare CLI
USAGE:
flarectl [global options] command [command options] [arguments...]
VERSION:
dev
COMMANDS:
ips, i Print Cloudflare IP ranges
user, u User information
zone, z Zone information
dns, d DNS records
user-agents, ua User-Agent blocking
pagerules, p Page Rules
railgun, r Railgun information
firewall, f Firewall
origin-ca-root-cert, ocrc Print Origin CA Root Certificate (in PEM format)
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--account-id value Optional account ID [$CF_ACCOUNT_ID]
--json show output as JSON instead of as a table (default: false)
--help, -h show help (default: false)
--version, -v print the version (default: false)
Did someone say developer environment?
So now, I think this is what you were looking for:
[brantley@clubsoda:~/src/cloudflare-go]$ flox develop -A cloudflare-go
[flox] λ go mod vendor
go: downloading github.com/urfave/cli/v2 v2.23.7
go: downloading github.com/olekukonko/tablewriter v0.0.5
go: downloading github.com/hashicorp/go-retryablehttp v0.7.1
go: downloading golang.org/x/time v0.0.0-20220224211638-0e9765cccd65
go: downloading golang.org/x/net v0.0.0-20220722155237-a158d28d115b
go: downloading github.com/stretchr/testify v1.8.1
go: downloading github.com/mattn/go-runewidth v0.0.13
go: downloading gopkg.in/yaml.v3 v3.0.1
go: downloading github.com/rivo/uniseg v0.2.0
go: downloading github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673
go: downloading github.com/cpuguy83/go-md2man/v2 v2.0.2
go: downloading github.com/russross/blackfriday/v2 v2.1.0
[flox] λ go build
[flox] λ
flox publish and more …
On the path to a developer environment we created a package, and we know we can build it with flox build
, but how can we install and use it in a flox environment?
This is where flox publish
comes into the picture, and you can use this to safely make precompiled signed and checksummed versions of your software easily available for customers to download (exactly as we make flox itself available for download in the flox/default
environment!).
I did exactly that as an exercise for this project, publishing the result to the github:flox-examples/floxpkgs repository - a transcript of the process is included below:
[brantley@clubsoda:~/src/cloudflare-go]$ flox publish
Select package for flox publish
> cloudflare-go
cloudflare-go-internal-tools
HINT: avoid selecting a package next time with:
$ flox publish -A cloudflare-go
build repository: https://github.com/flox-examples/cloudflare-go
package name: cloudflare-go
channel repository: git@github.com:flox-examples/floxpkgs
binary cache for upload: s3://flox-store-public
upload to: s3://flox-store-public
binary cache for download: https://cache.floxdev.com
download from: https://cache.floxdev.com
HINT: avoid having to answer these questions next time with:
$ flox publish -A cloudflare-go --build-repo https://github.com/flox-examples/cloudflare-go --channel-repo git@github.com:flox-examples/floxpkgs --upload-to s3://flox-store-public --download-from https://cache.floxdev.com
Cloning git@github.com:flox-examples/floxpkgs ...
Cloning into '/tmp/tmp.ILZ3PPvN8p'...
remote: Enumerating objects: 446, done.
remote: Counting objects: 100% (129/129), done.
remote: Compressing objects: 100% (83/83), done.
remote: Total 446 (delta 45), reused 106 (delta 23), pack-reused 317
Receiving objects: 100% (446/446), 88.79 KiB | 502.00 KiB/s, done.
Resolving deltas: 100% (143/143), done.
Building cloudflare-go ...
publishing render to catalog ...
flox publish completed
[master 1dbcff6] published x86_64-linux/stable/cloudflare-go/0.57.1-r2085.json
1 file changed, 12 insertions(+), 12 deletions(-)
Enumerating objects: 13, done.
Counting objects: 100% (13/13), done.
Delta compression using up to 8 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (7/7), 919 bytes | 919.00 KiB/s, done.
Total 7 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
To github.com:flox-examples/floxpkgs.git
d496671..1dbcff6 master -> master
… and having done that, users/customers can find and install this package with the following:
[brantley@clubsoda:~/src/cloudflare-go]$ flox subscribe flox-examples
Enter URL for 'flox-examples' channel: github:flox-examples/floxpkgs/master
[brantley@clubsoda:~/src/cloudflare-go]$ flox search -c flox-examples cloudflare-go
flox-examples.cloudflare-go
stable.flox-examples.cloudflare-go@0.57.1-r2085
[brantley@clubsoda:~/src/cloudflare-go]$ flox install -e cloudflare-test flox-examples.cloudflare-go
created generation 1
[brantley@clubsoda:~/src/cloudflare-go]$ flox activate -e cloudflare-test -- flarectl --version
flarectl version dev
I imagine that version “dev” is not what we’re looking for here, but I have no doubt that you will know where that value is coming from.
Hope this helps!
My apologies that this whistle-stop tour was somewhat light on background detail, but hopefully useful because it’s tailored to your specific example. Please let us know how you get on!