Skip to content

Commit

Permalink
simplify and drop features from nix build/shell
Browse files Browse the repository at this point in the history
Remove a bunch of stuff from the "dev" shell and rely on normal nix
builds/shell instead. This does drop some features like automatically
updating the vendorSha256, but hopefully the simpler setup means running
`go get -u`, `nix-build` and manually ensuring hash consistency feels
intuitive
  • Loading branch information
srhb committed May 3, 2021
1 parent 6c3a475 commit 46401e8
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 105 deletions.
23 changes: 2 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,32 +202,13 @@ machine2 = { ... }: {

All commands mentioned below is available in the nix-shell, if you run `nix-shell` with working dir = project root. The included `shell.nix` uses the latest `nixos-unstable` from GitHub by default, but you can override this by passing in another, eg. `nix-shell --arg nixpkgs '<nixpkgs>'` for your `$NIX_PATH` nixpkgs.


### Go dependency management

Run `make-deps` in order to:

1. (re-)install pinned dependencies in vendor-dir.
2. (re-)generate nix-packaging/deps.nix.

The former is done to support local dev, since IDE's often auto-import dependencies residing in /vendor.
The latter is used by the Nix go-builder.

Gopkg.toml specifies at which branch/tag each dependency is requested to be at.
Gopkg.lock specifies a concrete revision each dependency is pinned at.

If you want to bump dependencies to newest commit, run `make-deps update`, this will change Gopkg.lock and nix-packaging/deps.nix, both of which have to be git-committed.

If you make larger changes to the code base, you can delete both Gopkg.toml and Gopkg.lock and run `dep init` followed by `dep ensure` to create a fresh set of dependency tracking files. **don't forget to test** afterwards.
From within `nix-shell`, `go get -u` updates all go modules. Remember to update the `vendorSha256` in `./default.nix`

### Building the project with pinned dependencies

$ `nix-shell`

$ `make-build`

After successful build, `make-build` automatically invokes `make-env` to install the morph bin on the PATH of your nix-shell instance. Subsequently, it sources the morph bash-completion script to allow for completion of morph cli args and flags.

$ `nix-build --arg nixpkgs "builtins.fetchTarball https://github.com/NixOS/nixpkgs/archive/<rev>.tar.gz"`

## About the project

Expand Down
11 changes: 6 additions & 5 deletions nix-packaging/default.nix → default.nix
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
{ stdenv, fetchgit, buildGoModule, go-bindata, lib
{ nixpkgs ? import ./nixpkgs.nix
, pkgs ? import nixpkgs {}
, version ? "dev"
}:

buildGoModule rec {
pkgs.buildGoModule rec {
name = "morph-unstable-${version}";
inherit version;

nativeBuildInputs = [ go-bindata ];
nativeBuildInputs = with pkgs; [ go-bindata ];

src = import ./source.nix { inherit lib; };
src = pkgs.nix-gitignore.gitignoreSource [] ./.;

buildFlagsArray = ''
-ldflags=
-X
main.version=${version}
'';

vendorSha256 = "05rfvbqicr1ww4fjf6r1l8fb4f0rsv10vxndqny8wvng2j1rmmm6";
vendorSha256 = "0wv590gsbcfnikdz6sv4hzs5a91ldx2bmgr98yidvmiv1r4505pg";

postPatch = ''
go-bindata -pkg assets -o assets/assets.go data/
Expand Down
13 changes: 0 additions & 13 deletions nix-packaging/source.nix

This file was deleted.

2 changes: 2 additions & 0 deletions nixpkgs.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Intentionally impure for CI against nixos-unstable
builtins.fetchTarball "https://github.com/NixOS/nixpkgs/archive/nixos-unstable.tar.gz"
78 changes: 12 additions & 66 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -1,74 +1,20 @@
{ nixpkgs ? builtins.fetchTarball "https://github.com/NixOS/nixpkgs/archive/nixos-unstable.tar.gz"
{ nixpkgs ? import ./nixpkgs.nix
, pkgs ? import nixpkgs {}
}:

with pkgs;
let
packagingOut = "./nix-packaging";

shellHook = ''
if [[ -f ./result/bin/morph ]]; then
if [[ `${which} morph 2>&1 >/dev/null` ]]; then
export PATH=$PATH:$(pwd)/result/bin
fi
source <(morph --completion-script-bash)
fi
'';
makeEnv = writeScriptBin "make-env" (''
#!${bashInteractive}/bin/bash
'' + shellHook);
makeDeps = writeShellScriptBin "make-deps" ''
set -e
export GO111MODULE=on
# update the modules if requested
if [ "$1" == "update" ]; then
${go}/bin/go get -u
source="$( nix eval --raw '(with import ${nixpkgs} {}; import ./nix-packaging/source.nix { inherit lib; })' )"
# compute the sha256 of the dependencies
pushd "$source" >/dev/null
export GOPATH="$(mktemp -d)" GOCACHE="$(mktemp -d)"
${go}/bin/go mod download
sha256="$( ${nix}/bin/nix hash-path --base32 "$GOPATH/pkg/mod/cache/download" | tr -d '\n' )"
popd >/dev/null
# replace the sha256 in the default.nix
sed -e "s#vendorSha256.*#vendorSha256 = \"$sha256\";#" -i ${packagingOut}/default.nix
unset GOPATH GOCACHE
fi
# Populate /vendor (for convenience in local dev)
${go}/bin/go mod vendor
unset GO111MODULE
'';
makeBuild = writeShellScriptBin "make-build" ''
set -e
${nix}/bin/nix-build -E 'with import ${nixpkgs} {};
callPackage ./nix-packaging/default.nix {}' $@
make-env
morph = pkgs.callPackage ./default.nix {};
gen-assets = pkgs.writeShellScriptBin "gen-assets" ''
${pkgs.go-bindata}/bin/go-bindata -pkg assets -o assets/assets.go data/
'';
in
# Change to mkShell once that hits stable!
stdenv.mkDerivation {
name = "morph-build-env";

buildInputs = [
bashInteractive
git
makeEnv
makeDeps
makeBuild
nix
nix-prefetch-git
openssh
];
pkgs.mkShell {
buildInputs = [
gen-assets
];

inherit shellHook;
}
inputsFrom = [
morph
];
}

0 comments on commit 46401e8

Please sign in to comment.