-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: loon nix build
#54
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
on: [push, pull_request] | ||
name: Populate Cache | ||
jobs: | ||
populate: | ||
name: "${{ matrix.dependency }} (${{ matrix.platform }})" | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
platform: [ubuntu-latest, macos-latest] | ||
dependency: [crystal, golang, memcached, mysql, node, postgresql, redis, ruby] | ||
runs-on: ${{ matrix.platform }} | ||
steps: | ||
- name: Install Go | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: 1.14.x | ||
- name: Install Nix | ||
uses: cachix/install-nix-action@v10 | ||
with: | ||
nix_path: nixpkgs=channel:nixos-unstable | ||
- name: Install Cachix | ||
uses: cachix/cachix-action@v6 | ||
with: | ||
name: loon | ||
signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}' | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
- name: Build Loon | ||
run: go build | ||
- name: Build dependencies | ||
run: ./loon nix build --all ${{ matrix.dependency }} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package cli | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/andremedeiros/loon/internal/nix" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
var buildCmd = &cli.Command{ | ||
Name: "build", | ||
Usage: "Builds a dependency", | ||
Flags: []cli.Flag{ | ||
&cli.BoolFlag{Name: "all", Usage: "Builds all versions of a dependency"}, | ||
}, | ||
Action: func(c *cli.Context) error { | ||
all := c.Bool("all") | ||
if c.NArg() == 0 { | ||
return errors.New("specify a dependency name") | ||
} | ||
dep := c.Args().Get(0) | ||
|
||
if all { | ||
if _, err := nix.PackageFor(dep, "default"); err != nil { | ||
return err | ||
} | ||
for _, pkg := range nix.Packages() { | ||
if pkg.Name != dep { | ||
continue | ||
} | ||
|
||
if err := pkg.Build(); err != nil { | ||
return err | ||
} | ||
} | ||
} else { | ||
ver := c.Args().Get(1) | ||
if ver == "" { | ||
ver = "default" | ||
} | ||
pkg, err := nix.PackageFor(dep, ver) | ||
if err != nil { | ||
return err | ||
} | ||
return pkg.Build() | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
var nixCmd = &cli.Command{ | ||
Name: "nix", | ||
Usage: "Nix toolbox", | ||
Subcommands: []*cli.Command{buildCmd}, | ||
} | ||
|
||
func init() { | ||
appendCommand(nixCmd) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,14 @@ package nix | |
import ( | ||
"bytes" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/andremedeiros/loon/internal/catalog" | ||
"github.com/andremedeiros/loon/internal/executor" | ||
) | ||
|
||
type Package struct { | ||
|
@@ -32,6 +35,40 @@ func (p Package) DerivationPackages() string { | |
return buf.String() | ||
} | ||
|
||
func (p Package) Build() error { | ||
tmpl := ` | ||
{ pkgs ? import <nixpkgs> { } }: | ||
let | ||
inherit (pkgs) stdenv fetchurl mkShell; | ||
{{ .Derivation }} | ||
in rec { | ||
package = | ||
{{ .DerivationPackages }} | ||
; | ||
}` | ||
buf := bytes.NewBuffer([]byte{}) | ||
t, err := template.New("nix").Parse(tmpl) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if err != nil { | ||
// TODO(andremedeiros): figure out a better way | ||
panic(err) | ||
} | ||
t.Execute(buf, p) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
f, _ := ioutil.TempFile("", "default.nix") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You definitely want to check for an error here, per the documentation: https://golang.org/src/io/ioutil/tempfile.go?h=TempFile#L50 |
||
f.Write(buf.Bytes()) | ||
f.Close() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ISSUE: G104 (Severity: Medium) Remediation: 🤖 powered by PullRequest Automation 👋 verified by Denis R |
||
|
||
return executor.Execute([]string{ | ||
"nix-build", | ||
f.Name(), | ||
"-A", | ||
"package", | ||
"--no-out-link", | ||
}, | ||
executor.WithStdout(os.Stdout), | ||
executor.WithStderr(os.Stderr), | ||
) | ||
} | ||
|
||
func PackageFor(name string, version string) (Package, error) { | ||
for _, p := range Packages() { | ||
if p.Name == name && p.Version == version { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
derivation
struct uses a pointer receiver. Using the pointer receiver here would be my expectation so you are working with the current value at the addressp
instead of a copy, particularly since it is used in the file creation.However, there are caveats to the value of using a pointer which may outweigh its benefit. Here's a good piece by Dave Cheney for deeper insight: https://dave.cheney.net/tag/receivers
Denis R