refactor: nix build set up

The current nix set up was set up to use both nixos installation and
nix flake.

However, it was set up in a way that is isolated from the
active usage and build of bonfire. The following patch changes quite a
bit the original nix flake. The purpose is to use the same tooling as
that of the current setup for bonfire with just. In doing so, focusing
more on development environment for working with bonfire

The new set up takes the procedure of using just setup target with
WITH_DOCKER=no to imitate a similar local setup.

Note: while the nix flake does have sections to create nix packages and
even container images to build for bonfire, it is not goal of the
current PR to make those sections to work.

Ref: #405
This commit is contained in:
Alejandro Baez 2022-06-29 21:52:04 -05:00 committed by Mayel de Borniol
parent ee6b2f1485
commit 581903de4f
3 changed files with 143 additions and 53 deletions

5
.envrc Executable file → Normal file
View file

@ -1 +1,4 @@
use_flake
use flake
watch_file deps.nix
watch_file props.nix

25
flake.lock Executable file → Normal file
View file

@ -1,25 +0,0 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1625375231,
"narHash": "sha256-dbp2RRftypbAYad/xhDBBG0N7s1WQW+T4jazimvtvRo=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "8aff53d3cf23b5eb67c0ba9ba2392d694ade3930",
"type": "github"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

166
flake.nix Executable file → Normal file
View file

@ -1,33 +1,145 @@
{
description = "Bonfire!";
description = "Bonfire self contained build";
outputs = { self, nixpkgs }:
inputs = {
nixpkgs = { url = "github:NixOS/nixpkgs/nixpkgs-unstable"; };
flake-utils = { url = "github:numtide/flake-utils"; };
};
outputs = { self, nixpkgs, flake-utils, ... }:
let
# taken from https://github.com/ngi-nix/project-template/blob/master/flake.nix
# System types to support.
supportedSystems = [ "x86_64-linux" "x86_64-darwin" ];
# Helper function to generate an attrset '{ x86_64-linux = f "x86_64-linux"; ... }'.
forAllSystems = f: nixpkgs.lib.genAttrs supportedSystems (system: f system);
# Nixpkgs instantiated for supported system types.
nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; overlays = [ self.overlay ]; });
supportedSystems = [ "x86_64-linux" "aarch64-linux" ];
# props to hold settings to apply on this file like name and version
props = import ./props.nix;
# set elixir nix version
elixir_nix_version = elixir_version: builtins.replaceStrings [ "." ] [ "_" ] "elixir_${elixir_version}";
erlang_nix_version = erlang_version: "erlangR${erlang_version}";
in
{
overlay = final: prev: {
bonfire = import ./nix/package.nix { pkgs = final; inherit self; };
};
packages = forAllSystems (system:
{
inherit (nixpkgsFor.${system}) bonfire;
flake-utils.lib.eachSystem supportedSystems (system:
let
inherit (nixpkgs.lib) optional;
pkgs = import nixpkgs { inherit system; };
# project name for mix release
pname = props.app_name;
# project version for mix release
version = props.app_version;
# use ~r/erlangR[1-9]+/ for specific erlang release version
beamPackages = pkgs.beam.packagesWith pkgs.beam.interpreters.${erlang_nix_version props.erlang_release};
# all elixir and erlange packages
erlang = beamPackages.erlang;
# use ~r/elixir_1_[1-9]+/ major elixir version
elixir = beamPackages.${elixir_nix_version props.elixir_release};
elixir-ls = beamPackages.elixir_ls.overrideAttrs (oldAttrs: rec {
elixir = elixir;
});
defaultPackage = forAllSystems (system: self.packages.${system}.bonfire);
nixosModules.bonfire = import ./nix/module.nix;
devShell = forAllSystems
(system:
import ./nix/shell.nix {
pkgs = nixpkgsFor.${system};
}
);
};
hex = beamPackages.hex;
# use rebar from nix instead of fetch externally
rebar3 = beamPackages.rebar3;
rebar = beamPackages.rebar;
locality = pkgs.glibcLocales;
# needed to set libs for mix2nix
lib = pkgs.lib;
mix2nix = pkgs.mix2nix;
installHook = { release }: ''
export APP_VERSION="${version}"
export APP_NAME="${pname}"
export ELIXIR_RELEASE="${props.elixir_release}"
runHook preInstall
mix release --no-deps-check --path "$out" ${release}
runHook postInstall
'';
# src of the project
src = ./.;
# mix2nix dependencies
mixNixDeps = import ./deps.nix
{
inherit lib beamPackages;
};
# mix release definition
release-prod = beamPackages.mixRelease {
inherit src pname version mixNixDeps elixir;
mixEnv = "prod";
installPhase = installHook { release = "prod"; };
};
release-dev = beamPackages.mixRelease {
inherit src pname version mixNixDeps elixir;
mixEnv = "dev";
enableDebugInfo = true;
installPhase = installHook { release = "dev"; };
};
in
rec
{
# packages to build
packages = {
prod = release-prod;
dev = release-dev;
container = pkgs.dockerTools.buildImage {
name = pname;
tag = packages.prod.version;
# required extra packages to make release work
contents = [ packages.prod pkgs.coreutils pkgs.gnused pkgs.gnugrep ];
created = "now";
config.Entrypoint = [ "${packages.prod}/bin/prod" ];
config.Cmd = [ "version" ];
};
oci = pkgs.ociTools.buildContainer {
args = [ "${packages.prod}/bin/prod" ];
};
default = packages.prod;
};
# apps to run with nix run
apps = {
prod = flake-utils.lib.mkApp {
name = pname;
drv = packages.prod;
exePath = "/bin/prod";
};
dev = flake-utils.lib.mkApp {
name = "${pname}-dev";
drv = packages.dev;
exePath = "/bin/dev";
};
default = apps.prod;
};
devShells.default = pkgs.mkShell {
shellHook = ''
export APP_VERSION="${version}"
export APP_NAME="${pname}"
export ELIXIR_MAJOR_RELEASE="${props.elixir_release}"
export MIX_HOME="$PWD/.cache/mix";
export HEX_HOME="$PWD/.cache/hex";
export MIX_PATH="${hex}/lib/erlang/lib/hex/ebin"
export PATH="$MIX_PATH/bin:$HEX_HOME/bin:$PATH"
mix local.rebar --if-missing rebar3 ${rebar3}/bin/rebar3;
mix local.rebar --if-missing rebar ${rebar}/bin/rebar;
'';
buildInputs = [
elixir
erlang
mix2nix
locality
rebar3
rebar
] ++ optional pkgs.stdenv.isLinux pkgs.libnotify # For ExUnit Notifier on Linux.
++ optional pkgs.stdenv.isLinux pkgs.inotify-tools; # For file_system on Linux.
};
});
}