Skip to content

Commit

Permalink
Render a nix shell plus direnv config
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperSandro2000 committed Oct 8, 2024
1 parent 4ff2835 commit 23fd433
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
4 changes: 4 additions & 0 deletions internal/core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ type Metadata struct {
URL string `yaml:"url"`
}

type NixConfig struct {
ExtraPackages []string `yaml:"extraPackages"`
}

///////////////////////////////////////////////////////////////////////////////
// Helper functions

Expand Down
69 changes: 69 additions & 0 deletions internal/nix/nix-shell.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package nix

import (
"fmt"
"os"
"slices"
"strings"

"github.com/sapcc/go-bits/must"
"github.com/sapcc/go-makefile-maker/internal/core"
"github.com/sapcc/go-makefile-maker/internal/golang"
)

func RenderShell(cfg core.Configuration, sr golang.ScanResult, renderGoreleaserConfig bool) {
nixShellTemplate := `{ pkgs ? import <nixpkgs> { } }:
with pkgs;
mkShell {
nativeBuildInputs = [
%s
# keep this line if you use bash
bashInteractive
];
}
`

goVersionSlice := strings.Split(core.DefaultGoVersion, ".")
goPackage := fmt.Sprintf("go_%s_%s", goVersionSlice[0], goVersionSlice[1])
packages := []string{
goPackage,
"go-licence-detector",
"gotools # goimports",
}
if cfg.GolangciLint.CreateConfig {
packages = append(packages, "golangci-lint")
}
if renderGoreleaserConfig {
packages = append(packages, "goreleaser")
}
if sr.KubernetesController {
packages = append(packages, "kubernetes-controller-tools # controller-gen")
packages = append(packages, "setup-envtest")
}
if sr.UseGinkgo {
packages = append(packages, "ginkgo")
}
if sr.UsesPostgres {
packages = append(packages, "postgresql_"+core.DefaultPostgresVersion)
}
packages = append(packages, cfg.Nix.ExtraPackages...)

slices.Sort(packages)
packageList := ""
for _, pkg := range packages {
packageList += fmt.Sprintf(" %s\n", pkg)
}

nixShellFile := fmt.Sprintf(nixShellTemplate, packageList)
must.Succeed(os.WriteFile("shell.nix", []byte(nixShellFile), 0666))

must.Succeed(os.WriteFile(".envrc", []byte(`#!/usr/bin/env bash
if type -P lorri &>/dev/null; then
eval "$(lorri direnv)"
else
use nix
fi
`), 0666))
}
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/sapcc/go-makefile-maker/internal/golangcilint"
"github.com/sapcc/go-makefile-maker/internal/goreleaser"
"github.com/sapcc/go-makefile-maker/internal/makefile"
"github.com/sapcc/go-makefile-maker/internal/nix"
"github.com/sapcc/go-makefile-maker/internal/renovate"
)

Expand Down Expand Up @@ -62,6 +63,10 @@ func main() {
// Scan go.mod file for additional context information.
sr := golang.Scan()

renderGoreleaserConfig := (cfg.GoReleaser.CreateConfig == nil && cfg.GitHubWorkflow.Release.Enabled) || (cfg.GoReleaser.CreateConfig != nil && *cfg.GoReleaser.CreateConfig)

nix.RenderShell(cfg, sr, renderGoreleaserConfig)

// Render Makefile
if cfg.Makefile.Enabled == nil || *cfg.Makefile.Enabled {
for _, bin := range cfg.Binaries {
Expand All @@ -83,7 +88,7 @@ func main() {
}

// Render Goreleaser config file
if (cfg.GoReleaser.CreateConfig == nil && cfg.GitHubWorkflow.Release.Enabled) || (cfg.GoReleaser.CreateConfig != nil && *cfg.GoReleaser.CreateConfig) {
if renderGoreleaserConfig {
goreleaser.RenderConfig(cfg)
}

Expand Down

0 comments on commit 23fd433

Please sign in to comment.