Basic rules for setting up Nix dev environment

main
Ryan Rix 2023-03-02 12:51:33 -08:00
commit a76657cb6c
5 changed files with 204 additions and 0 deletions

1
.envrc Normal file
View File

@ -0,0 +1 @@
use nix

27
arcology.nix Normal file
View File

@ -0,0 +1,27 @@
{ lib, beamPackages, callPackage, ... }:
beamPackages.mixRelease rec {
pname = "arcology";
version = "0.0.1";
src = ./.;
dontStrip = true;
XDG_CACHE_HOME = "/tmp/elixir-cache";
mixNixDeps = import ./mix.nix {
inherit beamPackages lib;
overrides = (final: prev: {
exqlite = prev.exqlite.overrideAttrs (pprev: pprev // {
XDG_CACHE_HOME = "/tmp/elixir-cache";
});
});
};
meta = with lib; {
description = "Arcology Org-mode Web Engine";
homepage = "https://engine.arcology.garden";
license = licenses.unfree;
maintainers = with maintainers; [ rrix ];
};
}

5
default.nix Normal file
View File

@ -0,0 +1,5 @@
{ ... }:
let pkgs = import <nixpkgs> {};
in
pkgs.callPackage ./arcology.nix {}

133
setup.org Normal file
View File

@ -0,0 +1,133 @@
#+TITLE: Arcology Phoenix Setup
#+AUTO_TANGLE: t
* =nix-shell= for base dependencies
Install these packages from =nixpkgs=:
#+begin_src nix :noweb-ref packages
elixir_1_14
elixir_ls
inotify-tools
mix2nix
# probably will need this.... 🥴
nodePackages.node2nix
#+end_src
Set these basic environment variables in the shell:
#+begin_src nix :noweb-ref environment
LANG="C.UTF-8";
ERL_AFLAGS="-kernel shell_history enabled";
#+end_src
Put dependency files in =$PWD/.nix-shell=:
(/This is taken from [[https://elixirforum.com/t/flake-nix-phoenix-postgresql/52622][elixir forum]]/)
#+begin_src nix :noweb-ref environment
NIX_SHELL_DIR="$PWD/.nix-shell";
MIX_HOME="$NIX_SHELL_DIR/.mix";
MIX_ARCHIVES="$MIX_HOME/archives";
HEX_HOME="$NIX_SHELL_DIR/.hex";
PATH="$HEX_HOME/bin:$MIX_HOME/escripts:$MIX_HOME/bin:$PATH";
LIVEBOOK_HOME="$PWD";
#+end_src
Install a small wrapper script which will install =hex= package manager and the Phoenix mix tasks; run [[shell:setup-mix-phx][setup-mix-phx]] the first time this project is set up.
#+begin_src shell :noweb-ref packages
(writeScriptBin "setup-mix-phx" ''
${elixir}/bin/mix local.hex
${elixir}/bin/mix local.rebar
${elixir}/bin/mix archive.install hex phx_new
'')
#+end_src
Assemble all that in to a =pkgs.mkShell= for =nix-shell=
#+begin_src nix :noweb yes :tangle shell.nix
{ ... }:
let
pkgs = import <nixpkgs> {};
# import <arroyo> {};
in with pkgs; mkShell {
packages = [
<<packages>>
];
shellHook = ''
export <<environment>>
${elixir}/bin/mix --version
${elixir}/bin/iex --version
<<shellHook>>
'';
}
#+end_src
And set up [[id:45fc2a02-fcd0-40c6-a29e-897c0ee7b1c7][=direnv=]] (don't forget to =direnv allow=):
#+begin_src shell :tangle .envrc
use nix
#+end_src
This doesn't set up [[id:cce/wobserver/postgres][PostgreSQL]] or anything like that -- we're in [[roam:Sqlite]] country.
* =nix build= for deploying the project
I had a fully functional Arcology Phoenix a number of years back but [[https://discourse.nixos.org/t/nix-elixir-and-dependencies-with-port-compilation/11208][wasn't able]] to get =ecto_sqlite3= to build in NixOS. Better start on with that now!
Start with a stub =default.nix= that sets up a =callPackage=:
#+begin_src nix :tangle default.nix
{ ... }:
let pkgs = import <nixpkgs> {};
in
pkgs.callPackage ./arcology.nix {}
#+end_src
Then this thing can have its nix dependencies declared:
#+begin_src nix :tangle arcology.nix :noweb yes
{ lib, beamPackages, callPackage, ... }:
beamPackages.mixRelease rec {
pname = "arcology";
version = "0.0.1";
src = ./.;
dontStrip = true;
<<override-mix-deps>>
meta = with lib; {
description = "Arcology Org-mode Web Engine";
homepage = "https://engine.arcology.garden";
license = licenses.unfree;
maintainers = with maintainers; [ rrix ];
};
}
#+end_src
Nix's mix deps are provided by =mix2nix=, run [[shell:mix2nix > mix.nix]] when the =mix.exs= deps are updated;
this basically works, except that =exqlite= which =ecto_sqlite3= uses as a database driver. It will try to write "something" to =XDG_CACHE_HOME= ... I'm not sure why I need to define this twice -- defining it in only one of the root Arcology =mixRelease= or =exqlite='s =mixRelease= will cause the build to fail...
#+begin_src nix :noweb-ref override-mix-deps
XDG_CACHE_HOME = "/tmp/elixir-cache";
mixNixDeps = import ./mix.nix {
inherit beamPackages lib;
overrides = (final: prev: {
exqlite = prev.exqlite.overrideAttrs (pprev: pprev // {
XDG_CACHE_HOME = "/tmp/elixir-cache";
});
});
};
#+end_src

38
shell.nix Normal file
View File

@ -0,0 +1,38 @@
{ ... }:
let
pkgs = import <nixpkgs> {};
# import <arroyo> {};
in with pkgs; mkShell {
packages = [
elixir_1_14
elixir_ls
inotify-tools
mix2nix
# probably will need this.... 🥴
nodePackages.node2nix
(writeScriptBin "setup-mix-phx" ''
${elixir}/bin/mix local.hex
${elixir}/bin/mix local.rebar
${elixir}/bin/mix archive.install hex phx_new
'')
];
shellHook = ''
export LANG="C.UTF-8";
export ERL_AFLAGS="-kernel shell_history enabled";
export NIX_SHELL_DIR="$PWD/.nix-shell";
export MIX_HOME="$NIX_SHELL_DIR/.mix";
export MIX_ARCHIVES="$MIX_HOME/archives";
export HEX_HOME="$NIX_SHELL_DIR/.hex";
export PATH="$HEX_HOME/bin:$MIX_HOME/escripts:$MIX_HOME/bin:$PATH";
export LIVEBOOK_HOME="$PWD";
${elixir}/bin/mix --version
${elixir}/bin/iex --version
'';
}