commit d61feee579c05081ae68365654f009e815cbeff5
parent 3b07d33424499552164aceefb7462c0197f6f138
Author: Silas Brack <silasbrack@gmail.com>
Date: Mon, 14 Oct 2024 21:27:02 +0200
Getting VMs to work with flakes
Diffstat:
4 files changed, 199 insertions(+), 51 deletions(-)
diff --git a/apps/my_app/flake.lock b/apps/my_app/flake.lock
@@ -33,8 +33,41 @@
"type": "github"
}
},
+ "nixos-shell": {
+ "inputs": {
+ "nixpkgs": "nixpkgs"
+ },
+ "locked": {
+ "lastModified": 1728308049,
+ "narHash": "sha256-wjAkG8f5orZ4+wgXmEaFAJkVJS6ORqfgM0zzLEvbUNw=",
+ "owner": "Mic92",
+ "repo": "nixos-shell",
+ "rev": "9373ca9522f844a3b8029720c74f340e560e4462",
+ "type": "github"
+ },
+ "original": {
+ "owner": "Mic92",
+ "repo": "nixos-shell",
+ "type": "github"
+ }
+ },
"nixpkgs": {
"locked": {
+ "lastModified": 1628465643,
+ "narHash": "sha256-QSNw9bDq9uGUniQQtakRuw4m21Jxugm23SXLVgEV4DM=",
+ "owner": "NixOS",
+ "repo": "nixpkgs",
+ "rev": "6ef4f522d63f22b40004319778761040d3197390",
+ "type": "github"
+ },
+ "original": {
+ "id": "nixpkgs",
+ "ref": "nixos-unstable",
+ "type": "indirect"
+ }
+ },
+ "nixpkgs_2": {
+ "locked": {
"lastModified": 1728492678,
"narHash": "sha256-9UTxR8eukdg+XZeHgxW5hQA9fIKHsKCdOIUycTryeVw=",
"owner": "NixOS",
@@ -53,7 +86,8 @@
"inputs": {
"flake-utils": "flake-utils",
"nix-gleam": "nix-gleam",
- "nixpkgs": "nixpkgs"
+ "nixos-shell": "nixos-shell",
+ "nixpkgs": "nixpkgs_2"
}
},
"systems": {
diff --git a/apps/my_app/flake.nix b/apps/my_app/flake.nix
@@ -1,46 +1,63 @@
{
description = "My gleam monorepo";
+ inputs = {
+ nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
+ # nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
+ nixos-shell.url = "github:Mic92/nixos-shell";
+ flake-utils.url = "github:numtide/flake-utils";
+ nix-gleam.url = "github:arnarg/nix-gleam";
+ };
- inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
- inputs.flake-utils.url = "github:numtide/flake-utils";
- inputs.nix-gleam.url = "github:arnarg/nix-gleam";
-
- outputs = {
- self,
- nixpkgs,
- flake-utils,
- nix-gleam,
- }: (
- flake-utils.lib.eachDefaultSystem
- (system: let
- pkgs = import nixpkgs {
- inherit system;
- overlays = [
- nix-gleam.overlays.default
- ];
- };
- in {
- packages.default = pkgs.buildGleamApplication {
- src = ./.;
- erlangPackage = pkgs.erlang_27;
- };
- devShells.system.default = {
- buildInputs = [
- pkgs.gleam
- pkgs.erlang_27
- pkgs.rebar3
- pkgs.inotify-tools
- pkgs.nodejs_22
+ outputs =
+ {
+ self,
+ nixpkgs,
+ nixos-shell,
+ flake-utils,
+ nix-gleam,
+ }:
+ (flake-utils.lib.eachDefaultSystem (
+ system:
+ let
+ pkgs = import nixpkgs {
+ inherit system;
+ overlays = [
+ nix-gleam.overlays.default
+ ];
+ };
+ in
+ {
+ packages.default = pkgs.buildGleamApplication {
+ src = ./.;
+ erlangPackage = pkgs.erlang_27;
+ };
+ devShells.system.default = {
+ buildInputs = [
+ pkgs.gleam
+ pkgs.erlang_27
+ pkgs.rebar3
+ pkgs.inotify-tools
+ pkgs.nodejs_22
+ ];
+ shellHook = ''
+ export PORT=52393
+ export PGHOST="127.0.0.1"
+ export PGPASSWORD="postgres"
+ export PGPORT=44537
+ export PGUSER="silas"
+ export PGDB="mydb"
+ '';
+ };
+ }
+ ))
+ // {
+ nixosConfigurations.vm = nixpkgs.lib.nixosSystem {
+ system = "x86_64-linux";
+ modules = [
+ (import ./vm.nix)
+ nixos-shell.nixosModules.nixos-shell
];
- shellHook = ''
- export PORT=52393
- export PGHOST="127.0.0.1"
- export PGPASSWORD="postgres"
- export PGPORT=44537
- export PGUSER="silas"
- export PGDB="mydb"
- '';
};
- })
- );
+ }
+ ;
}
diff --git a/apps/my_app/vm.nix b/apps/my_app/vm.nix
@@ -0,0 +1,63 @@
+# {
+# inputs = {
+# my_app.url = "path:.";
+# nixos-shell.url = "github:Mic92/nixos-shell";
+# };
+# outputs.nixosConfigurations =
+{ pkgs, lib, my_app, ... }:
+ let
+ port = 8008;
+ pg_port = 44537;
+ in {
+ system.stateVersion = "24.11";
+
+ environment.sessionVariables = rec {
+ PORT = port;
+ PG_PORT = pg_port;
+ };
+
+ imports = [ ./module.nix ];
+
+ services.my_app = {
+ enable = true;
+ package = my_app.packages."x86_64-linux".default;
+ port = port;
+ };
+ # virtualisation.forwardPorts = [ port ];
+
+ # Enable postgres
+ services.postgresql = {
+ enable = true;
+ package = pkgs.postgresql_16;
+ extraPlugins = ps: with ps; [ pgvector ];
+ ensureDatabases = [ "mydb" ];
+ ensureUsers = [
+ {
+ name = "datascientist";
+ }
+ {
+ name = "silas";
+ }
+ ];
+ authentication = pkgs.lib.mkOverride 10 ''
+ #TYPE DATABASE USER AUTH-METHOD OPTIONAL_IDENT_MAP
+ local all all peer map=superuser_map
+ #TYPE DATABASE USER ADDRESS AUTH-METHOD
+ host all all 127.0.0.1/32 trust
+ host all all ::1/128 trust
+ '';
+ identMap = ''
+ # MAPNAME SYSTEM-USERNAME PG-USERNAME
+ superuser_map root postgres
+ superuser_map postgres postgres
+ superuser_map /^(.*)$ \1
+ '';
+ initialScript = pkgs.writeText "backend-initScript" ''
+ GRANT ALL PRIVILEGES ON DATABASE mydb TO silas;
+ '';
+ settings = {
+ port = lib.mkForce pg_port;
+ };
+ };
+ }
+# }
diff --git a/flake.lock b/flake.lock
@@ -22,11 +22,12 @@
"inputs": {
"flake-utils": "flake-utils",
"nix-gleam": "nix-gleam",
- "nixpkgs": "nixpkgs"
+ "nixos-shell": "nixos-shell",
+ "nixpkgs": "nixpkgs_2"
},
"locked": {
"lastModified": 1,
- "narHash": "sha256-j8bSHeeUjQT6X76t2O5np+cDMgxezME7YDkvV5mHnSQ=",
+ "narHash": "sha256-dwK1tgzMFluDnKarrZFjOagGBJkLQTOaIgr2B2p41cM=",
"path": "./apps/my_app",
"type": "path"
},
@@ -50,20 +51,37 @@
"type": "github"
}
},
+ "nixos-shell": {
+ "inputs": {
+ "nixpkgs": "nixpkgs"
+ },
+ "locked": {
+ "lastModified": 1728308049,
+ "narHash": "sha256-wjAkG8f5orZ4+wgXmEaFAJkVJS6ORqfgM0zzLEvbUNw=",
+ "owner": "Mic92",
+ "repo": "nixos-shell",
+ "rev": "9373ca9522f844a3b8029720c74f340e560e4462",
+ "type": "github"
+ },
+ "original": {
+ "owner": "Mic92",
+ "repo": "nixos-shell",
+ "type": "github"
+ }
+ },
"nixpkgs": {
"locked": {
- "lastModified": 1728492678,
- "narHash": "sha256-9UTxR8eukdg+XZeHgxW5hQA9fIKHsKCdOIUycTryeVw=",
+ "lastModified": 1628465643,
+ "narHash": "sha256-QSNw9bDq9uGUniQQtakRuw4m21Jxugm23SXLVgEV4DM=",
"owner": "NixOS",
"repo": "nixpkgs",
- "rev": "5633bcff0c6162b9e4b5f1264264611e950c8ec7",
+ "rev": "6ef4f522d63f22b40004319778761040d3197390",
"type": "github"
},
"original": {
- "owner": "NixOS",
+ "id": "nixpkgs",
"ref": "nixos-unstable",
- "repo": "nixpkgs",
- "type": "github"
+ "type": "indirect"
}
},
"nixpkgs-stable": {
@@ -100,6 +118,22 @@
},
"nixpkgs_3": {
"locked": {
+ "lastModified": 1728492678,
+ "narHash": "sha256-9UTxR8eukdg+XZeHgxW5hQA9fIKHsKCdOIUycTryeVw=",
+ "owner": "NixOS",
+ "repo": "nixpkgs",
+ "rev": "5633bcff0c6162b9e4b5f1264264611e950c8ec7",
+ "type": "github"
+ },
+ "original": {
+ "owner": "NixOS",
+ "ref": "nixos-unstable",
+ "repo": "nixpkgs",
+ "type": "github"
+ }
+ },
+ "nixpkgs_4": {
+ "locked": {
"lastModified": 1728093190,
"narHash": "sha256-CAZF2NRuHmqTtRTNAruWpHA43Gg2UvuCNEIzabP0l6M=",
"owner": "NixOS",
@@ -117,13 +151,13 @@
"root": {
"inputs": {
"my_app": "my_app",
- "nixpkgs": "nixpkgs_2",
+ "nixpkgs": "nixpkgs_3",
"sops-nix": "sops-nix"
}
},
"sops-nix": {
"inputs": {
- "nixpkgs": "nixpkgs_3",
+ "nixpkgs": "nixpkgs_4",
"nixpkgs-stable": "nixpkgs-stable"
},
"locked": {