Skip to content

Commit

Permalink
feat: use flake-parts
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobLichterfeld committed Sep 30, 2024
1 parent cef1dec commit c271d18
Show file tree
Hide file tree
Showing 4 changed files with 296 additions and 296 deletions.
15 changes: 0 additions & 15 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

286 changes: 5 additions & 281 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,292 +11,16 @@
};

outputs =
inputs@{
self,
nixpkgs,
flake-parts,
devenv,
systems,
treefmt-nix,
}:
flake-parts.lib.mkFlake { inherit inputs; } {
imports = [ inputs.treefmt-nix.flakeModule ];
inputs:
inputs.flake-parts.lib.mkFlake { inherit inputs; } {

systems = [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
];
perSystem =
{
config,
self',
inputs',
pkgs,
system,
...
}:

let
inherit (pkgs.lib) optional optionals;
pkgs = nixpkgs.legacyPackages.${system};

elixir = pkgs.beam.packages.erlang.elixir_1_16;
beamPackages = pkgs.beam.packagesWith pkgs.beam.interpreters.erlang;

src = ./.;
version = builtins.readFile ./VERSION;
pname = "teslamate";

mixFodDeps = beamPackages.fetchMixDeps {
TOP_SRC = src;
pname = "${pname}-mix-deps";
inherit src version;
hash = "sha256-ukE7PKrsRaw6TWDSqTJljsPJQedi39mZgGFrMINEJlw=";
# hash = pkgs.lib.fakeHash;
};

nodejs = pkgs.nodejs;
nodePackages = pkgs.buildNpmPackage {
name = "teslamate";
src = ./assets;
npmDepsHash = "sha256-05AKPyms4WP8MHBqWMup8VXR3a1tv/f/7jT8c6EpWBw=";
# npmDepsHash = pkgs.lib.fakeHash;
dontNpmBuild = true;
inherit nodejs;

installPhase = ''
mkdir $out
cp -r node_modules $out
ln -s $out/node_modules/.bin $out/bin
rm $out/node_modules/phoenix
ln -s ${mixFodDeps}/phoenix $out/node_modules
rm $out/node_modules/phoenix_html
ln -s ${mixFodDeps}/phoenix_html $out/node_modules
rm $out/node_modules/phoenix_live_view
ln -s ${mixFodDeps}/phoenix_live_view $out/node_modules
'';
};

cldr = pkgs.fetchFromGitHub {
owner = "elixir-cldr";
repo = "cldr";
rev = "v2.37.5";
sha256 = "sha256-T5Qvuo+xPwpgBsqHNZYnTCA4loToeBn1LKTMsDcCdYs=";
# sha256 = pkgs.lib.fakeHash;
};

pkg = beamPackages.mixRelease {
TOP_SRC = src;
inherit
pname
version
elixir
src
mixFodDeps
;

LOCALES = "${cldr}/priv/cldr";

postBuild = ''
ln -sf ${mixFodDeps}/deps deps
ln -sf ${nodePackages}/node_modules assets/node_modules
export PATH="${pkgs.nodejs}/bin:${nodePackages}/bin:$PATH"
${nodejs}/bin/npm run deploy --prefix ./assets
# for external task you need a workaround for the no deps check flag
# https://github.com/phoenixframework/phoenix/issues/2690
mix do deps.loadpaths --no-deps-check, phx.digest
mix phx.digest --no-deps-check
'';

meta = {
mainProgram = "teslamate";
};
};

postgres_port = 7000;
mosquitto_port = 7001;
process_compose_port = 7002;

psql = pkgs.writeShellScriptBin "teslamate_psql" ''
exec "${pkgs.postgresql}/bin/psql" --host "$DATABASE_HOST" --user "$DATABASE_USER" --port "$DATABASE_PORT" "$DATABASE_NAME" "$@"
'';
mosquitto_sub = pkgs.writeShellScriptBin "teslamate_sub" ''
exec "${pkgs.mosquitto}/bin/mosquitto_sub" -h "$MQTT_HOST" -p "$MQTT_PORT" -u "$MQTT_USERNAME" -P "$MQTT_PASSWORD" "$@"
'';

devShell = devenv.lib.mkShell {
inherit inputs pkgs;
modules = with pkgs; [
{
packages =
[
elixir
elixir_ls
node2nix
nodejs
prefetch-npm-deps
# for dashboard scripts
jq
psql
mosquitto
mosquitto_sub
# config.treefmt.build.wrapper
]
# ++ builtins.attrValues config.treefmt.build.programs
++ optional stdenv.isLinux [
inotify-tools
glibcLocales
]
++ optional stdenv.isDarwin terminal-notifier
++ optionals stdenv.isDarwin (
with darwin.apple_sdk.frameworks;
[
CoreFoundation
CoreServices
]
);
enterShell = ''
export LOCALES="${cldr}/priv/cldr";
export PORT="4000"
export ENCRYPTION_KEY="your_secure_encryption_key_here"
export DATABASE_USER="teslamate"
export DATABASE_PASS="your_secure_password_here"
export DATABASE_NAME="teslamate"
export DATABASE_HOST="127.0.0.1"
export DATABASE_PORT="${toString postgres_port}"
export MQTT_HOST="127.0.0.1"
export MQTT_PORT="${toString mosquitto_port}"
export RELEASE_COOKIE="1234567890123456789"
export TZDATA_DIR="$PWD/tzdata"
mix deps.get
'';
enterTest = ''
mix test
'';
processes.mqtt = {
exec = "${pkgs.mosquitto}/bin/mosquitto -p ${toString mosquitto_port}";
};
process.process-compose = {
port = process_compose_port;
tui = true;
};
services.postgres = {
enable = true;
package = pkgs.postgresql_16;
listen_addresses = "127.0.0.1";
port = postgres_port;
initialDatabases = [ { name = "teslamate"; } ];
initialScript = ''
CREATE USER teslamate with encrypted password 'your_secure_password_here';
GRANT ALL PRIVILEGES ON DATABASE teslamate TO teslamate;
ALTER USER teslamate WITH SUPERUSER;
'';
};
}
];

};

moduleTest =
(nixpkgs.lib.nixos.runTest {
hostPkgs = pkgs;
defaults.documentation.enable = false;
imports = [
{
name = "teslamate";
nodes.server = {
nixosModules.default = import ./module.nix { inherit self; };
imports = [ self.nixosModules.default ];
services.teslamate = {
enable = true;
secretsFile = builtins.toFile "teslamate.env" ''
ENCRYPTION_KEY=123456789
DATABASE_PASS=123456789
RELEASE_COOKIE=123456789
'';
postgres.enable = true;
grafana.enable = true;
};
};

testScript = ''
server.wait_for_open_port(4000)
'';
}
];
}).config.result;
in
{
packages = {
devenv-up = devShell.config.procfileScript;
default = pkg;
};
devShells.default = devShell;

# for `nix fmt`
treefmt.config = {
flakeFormatter = true;
flakeCheck = true;
projectRootFile = "flake.nix";

# we really need to mirror the treefmt.toml as we can't use it directly
global.excludes = [
"*.gitignore"
"*.dockerignore"
".envrc"
"*.node-version"
"Dockerfile"
"grafana/Dockerfile"
"Makefile"
"VERSION"
"LICENSE"
"*.metadata"
"*.manifest"
"*.webmanifest"
"*.dat"
"*.lock"
"*.txt"
"*.csv"
"*.ico"
"*.png"
"*.svg"
"*.properties"
"*.xml"
"*.po"
"*.pot"
"*.json.example"
"*.typos.toml"
"treefmt.toml"
"grafana/dashboards/*.json" # we use the grafana export style
];
programs.mix.enable = true;
settings.formatter.mix-format.includes = [
"*.ex"
"*.exs"
"*.{heex,eex}"
];
# run shellcheck first
programs.shellcheck.enable = true;
settings.formatter.shellcheck.priority = 0; # default is 0, but we set it here for clarity

# shfmt second
programs.shfmt.enable = true;
settings.formatter.shfmt.priority = 1;

programs.prettier.enable = true;

programs.nixpkgs-fmt.enable = true;
};
# for `nix flake check`
checks = {
default = moduleTest;
formatting = pkgs.treefmt.check;
};
};
# See ./nix/modules/*.nix for the modules that are imported here.
imports = with builtins; map (fn: ./nix/modules/${fn}) (attrNames (readDir ./nix/modules));
};
}
65 changes: 65 additions & 0 deletions nix/modules/formatter.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{ inputs, ... }:
{
imports = [
inputs.treefmt-nix.flakeModule
];
perSystem =
{ config, pkgs, ... }:
{
# Auto formatters. This also adds a flake check to ensure that the
# source tree was auto formatted.
treefmt.config = {
flakeFormatter = true;
flakeCheck = true;
projectRootFile = "flake.nix";

# we really need to mirror the treefmt.toml as we can't use it directly
global.excludes = [
"*.gitignore"
"*.dockerignore"
".envrc"
"*.node-version"
"Dockerfile"
"grafana/Dockerfile"
"Makefile"
"VERSION"
"LICENSE"
"*.metadata"
"*.manifest"
"*.webmanifest"
"*.dat"
"*.lock"
"*.txt"
"*.csv"
"*.ico"
"*.png"
"*.svg"
"*.properties"
"*.xml"
"*.po"
"*.pot"
"*.json.example"
"*.typos.toml"
"treefmt.toml"
"grafana/dashboards/*.json" # we use the grafana export style
];
programs.mix.enable = true;
settings.formatter.mix-format.includes = [
"*.ex"
"*.exs"
"*.{heex,eex}"
];
# run shellcheck first
programs.shellcheck.enable = true;
settings.formatter.shellcheck.priority = 0; # default is 0, but we set it here for clarity

# shfmt second
programs.shfmt.enable = true;
settings.formatter.shfmt.priority = 1;

programs.prettier.enable = true;

programs.nixpkgs-fmt.enable = true;
};
};
}
Loading

0 comments on commit c271d18

Please sign in to comment.