diff --git a/flake.lock b/flake.lock index 13821c5..c2af4a6 100644 --- a/flake.lock +++ b/flake.lock @@ -20,11 +20,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1710889954, - "narHash": "sha256-Pr6F5Pmd7JnNEMHHmspZ0qVqIBVxyZ13ik1pJtm2QXk=", + "lastModified": 1724334015, + "narHash": "sha256-5sfvc0MswIRNdRWioUhG58rGKGn2o90Ck6l6ClpwQqA=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "7872526e9c5332274ea5932a0c3270d6e4724f3b", + "rev": "6d204f819efff3d552a88d0a44b5aaaee172b784", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index a7b0aaf..4c617a0 100644 --- a/flake.nix +++ b/flake.nix @@ -19,6 +19,7 @@ system = "x86_64-linux"; modules = [ (import ./modules/overlay.nix) + ./modules/loomio.nix ./hosts/test/configuration.nix ]; }; diff --git a/hosts/test/configuration.nix b/hosts/test/configuration.nix index 23dbf96..92492d3 100644 --- a/hosts/test/configuration.nix +++ b/hosts/test/configuration.nix @@ -19,11 +19,10 @@ users.users.nixosvmtest.group = "nixosvmtest"; users.groups.nixosvmtest = {}; - users.groups.loomio = {}; - users.users.loomio = { - description = "User to run loomio"; - group = "loomio"; - isSystemUser = true; + services.loomio = { + enable = true; + encyrptedCredentials = ./loomio-credentials.yml.enc; + credentialEncryptionKeyFile = ./loomio-master.key; }; services.postgresql = { @@ -32,23 +31,9 @@ ensureUsers = [{ name = "loomio"; ensureDBOwnership = true; - # ensurePermissions = { "DATABASE loomio" = "ALL PRIVILEGES"; }; + ensureClauses.superuser = true; }]; }; - systemd.services.loomio = { - enable = true; - after = [ "basic.target" ]; - - serviceConfig = { - User = "loomio"; - Restart = "always"; - ExecStart = "${pkgs.loomio}/bin/loomio"; - }; - environment = { - DATABASE_URL = "postgresql://localhost/loomio"; - }; - }; - system.stateVersion = "23.11"; } diff --git a/hosts/test/loomio-credentials.yml.enc b/hosts/test/loomio-credentials.yml.enc new file mode 100644 index 0000000..45640a9 --- /dev/null +++ b/hosts/test/loomio-credentials.yml.enc @@ -0,0 +1 @@ +1LR0cmifVLl04EAAtWPaoBhy85l6u7pGPjiSheUe/vkKbu3S3MK4XV6vBOmGnki4yMrOvdpvpLomnlCxCxEyOYcDRtBh3LkgZ3+9Bx37IcRHfgobzWVG/ik1/ZA4w2zbo8u/oJD6/gtuJNXBTkWNgpo4/WCJddxrzbF1Jo/FawT/ecwPO5898x/VBZn79rMFqcSJ2NKY4noOhOr2+EosFBjEkU7T5QVag64WQDT5/Np2vVhFWPvCgyoXziKTjBpCIUE9dDcA7HFIPgZ5nZXIwdSmVdAN3cA1zDlQMGqA27R4tt9zuRkPCvnfNvfuuVp9FHuok7tUTFPNR6v6NPzvHNceHugUvFF9zu33MAl8tH9j5W3Wd7zPVxuYp9nU8nHPmQF5DlPUoqUERLq/0biF7+F/4KGkNl+DFkZW--EGCnUuZJQoZORDG3--UsPRfjEX4LFxe6ECg8uR9Q== \ No newline at end of file diff --git a/hosts/test/loomio-master.key b/hosts/test/loomio-master.key new file mode 100644 index 0000000..7073936 --- /dev/null +++ b/hosts/test/loomio-master.key @@ -0,0 +1 @@ +dd587169f25e5ed7e391a511da13f491 \ No newline at end of file diff --git a/modules/loomio.nix b/modules/loomio.nix index 3c18730..a3b89b2 100644 --- a/modules/loomio.nix +++ b/modules/loomio.nix @@ -1,12 +1,116 @@ -{lib, ...}: -{ +{pkgs, lib, config, ...}: +let + cfg = config.services.loomio; + package = pkgs.loomio; + + env = { + RAILS_ENV = "production"; + BUNDLE_FORCE_RUBY_PLATFORM = "true"; + + LOOMIO_DATABASE = "loomio"; + LOOMIO_ENCRYPTED_CREDENTIALS_PATH = cfg.encyrptedCredentials; + }; + cfgService = { + User = "loomio"; + Group = "loomio"; + WorkingDirectory = package; + StateDirectory = "loomio"; + }; +in { imports = []; options = { - enable = lib.mkEnableOption "loomio"; + services.loomio = { + enable = lib.mkEnableOption "loomio"; + encyrptedCredentials = lib.mkOption { + description = "Credentials required to run loomio"; + type = lib.types.path; + }; + credentialEncryptionKeyFile = lib.mkOption { + description = "File containing encryption key for the encyptedCredentials"; + type = lib.types.path; + }; + }; }; config = { + users.groups.loomio = {}; + users.users.loomio = { + description = "User to run loomio"; + group = "loomio"; + isSystemUser = true; + }; + + systemd.services.loomio-init-dirs = { + enable = true; + after = ["network.target" ]; + serviceConfig = cfgService // { + Type = "oneshot"; + }; + script = '' + cat > /var/lib/loomio/.secrets_env < + adapter: postgresql + database: <%= ENV['LOOMIO_DATABASE'] %> + host: /var/run/postgresql ''; + in stdenv.mkDerivation { name = "loomio"; inherit src; + nativeBuildInputs = [ gems gems.wrappedRuby ]; buildInputs = [gems ruby_3_2]; + buildPhase = '' + runHook preBuild + + cp ${databaseConfig} config/database.yml + cp ${./puma.rb} config/puma.rb + + export BUNDLE_FORCE_RUBY_PLATFORM=true + ${gems}/bin/bundle exec bootsnap precompile --gemfile app/ lib/ + + patchShebangs bin/ + + for b in $(ls ${gems}/bin/) + do + if [ ! -f bin/$b ]; then + ln -s ${gems}/bin/$b bin/$b + fi + done + + runHook postBuild + ''; + installPhase = '' - mkdir -p $out/{bin,share/loomio} - cp -r * $out/share/loomio - bin=$out/bin/loomio - cp ${databaseConfig} $out/share/loomio/config/database.yml - cat > $bin <