diff --git a/default.nix b/default.nix index 757a300..9b335cd 100644 --- a/default.nix +++ b/default.nix @@ -1,69 +1,97 @@ -{ stdenv, lib, makeWrapper -, fetchFromGitHub, gitignoreSource -, ruby, bundler, bundix, bundlerEnv, defaultGemConfig -, rustPlatform, cargo, rustc, fetchgit -, libsodium, libopus, imagemagick }: +{ + stdenv, + lib, + makeWrapper, + fetchFromGitHub, + gitignoreSource, + ruby, + bundler, + bundix, + bundlerEnv, + defaultGemConfig, + rustPlatform, + cargo, + rustc, + fetchgit, + libsodium, + libopus, + imagemagick, +}: let - env = bundlerEnv { - name = "qbot-bundler-env"; - gemdir = ./.; - inherit ruby; + env = bundlerEnv { + name = "qbot-bundler-env"; + gemdir = ./.; + inherit ruby; - gemConfig = defaultGemConfig // { - tantiny = attrs: { - cargoDeps = rustPlatform.fetchCargoTarball { - src = fetchgit { - inherit (attrs.source) - url rev sha256 fetchSubmodules; - }; + gemConfig = defaultGemConfig // { + tantiny = attrs: { + cargoDeps = rustPlatform.fetchCargoTarball { + src = fetchgit { + inherit (attrs.source) + url + rev + sha256 + fetchSubmodules + ; + }; - sha256 = "JlPkdrU2fq+0v/2QJnqtSEv3bqiJbdAvzK3NrrMdY8A="; - }; + sha256 = "JlPkdrU2fq+0v/2QJnqtSEv3bqiJbdAvzK3NrrMdY8A="; + }; - nativeBuildInputs = [ - rustPlatform.cargoSetupHook cargo rustc - ]; + nativeBuildInputs = [ + rustPlatform.cargoSetupHook + cargo + rustc + ]; - postUnpack = '' - mv .cargo tantiny* - ''; - }; - }; - }; + postUnpack = '' + mv .cargo tantiny* + ''; + }; + }; + }; -in stdenv.mkDerivation rec { - name = "qbot"; +in +stdenv.mkDerivation rec { + name = "qbot"; - src = gitignoreSource ./.; + src = gitignoreSource ./.; - nativeBuildInputs = [ makeWrapper ]; - buildInputs = [ env.wrappedRuby imagemagick ]; - propagatedBuildInputs = [ libopus libsodium ]; + nativeBuildInputs = [ makeWrapper ]; + buildInputs = [ + env.wrappedRuby + imagemagick + ]; + propagatedBuildInputs = [ + libopus + libsodium + ]; - passthru = { - fontconfigFile = "${src}/share/fc-config.xml"; - binPath = lib.makeBinPath buildInputs; - libPath = lib.makeLibraryPath propagatedBuildInputs; - }; + passthru = { + fontconfigFile = "${src}/share/fc-config.xml"; + binPath = lib.makeBinPath buildInputs; + libPath = lib.makeLibraryPath propagatedBuildInputs; + }; - installPhase = let - inherit (passthru) binPath libPath fontconfigFile; - in '' - mkdir -p $out/{bin,share} - cp -r . $out/share/qbot + installPhase = + let + inherit (passthru) binPath libPath fontconfigFile; + in + '' + mkdir -p $out/{bin,share} + cp -r . $out/share/qbot + makeWrapper $out/share/qbot/qbot $out/bin/qbot \ + --set FONTCONFIG_FILE '${fontconfigFile}' \ + --prefix PATH : '${binPath}' \ + --prefix LD_LIBRARY_PATH : '${libPath}' + ''; - makeWrapper $out/share/qbot/qbot $out/bin/qbot \ - --set FONTCONFIG_FILE '${fontconfigFile}' \ - --prefix PATH : '${binPath}' \ - --prefix LD_LIBRARY_PATH : '${libPath}' - ''; - - meta = with lib; { - description = "General purpose Discord bot"; - homepage = "https://github.com/arch-community/qbot"; - license = licenses.agpl3Plus; - maintainers = with maintainers; [ anna328p ]; - mainProgram = "qbot"; - }; + meta = with lib; { + description = "General purpose Discord bot"; + homepage = "https://github.com/arch-community/qbot"; + license = licenses.agpl3Plus; + maintainers = with maintainers; [ anna328p ]; + mainProgram = "qbot"; + }; } diff --git a/flake.nix b/flake.nix index 88bd695..ad8aec1 100644 --- a/flake.nix +++ b/flake.nix @@ -1,118 +1,132 @@ rec { - description = "qbot flake"; - - inputs = { - nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable-small"; - - # TODO: remove once nixpkgs#272969 is merged - rust-overlay.url = "github:oxalica/rust-overlay"; - - gitignore.url = "github:hercules-ci/gitignore.nix"; - gitignore.inputs.nixpkgs.follows = "nixpkgs"; - }; - - nixConfig = { - extra-substituters = "https://qbot.cachix.org"; - extra-trusted-public-keys = - "qbot.cachix.org-1:xkDcKYI5RucucGnOvREbPYj3+Ld1iVco0UFNQj1JVc8="; - }; - - outputs = { self - , nixpkgs - , rust-overlay - , gitignore - }@flakes: - let - # forEachSystem : (Str -> Set Any) -> Set (Set Any); - forEachSystem = let - inherit (nixpkgs.lib) genAttrs systems; - in - genAttrs systems.flakeExposed; - - mkQBotArgs = pkgs: rec { - ruby = pkgs.ruby_3_2; - - # TODO: remove once nixpkgs#272969 is merged - rustc = rust-overlay.packages.${pkgs.system}.rust; - cargo = rustc; - - inherit (gitignore.lib) gitignoreSource; - }; - - commonEnv = system: rec { - pkgs = import nixpkgs { - inherit system; - config.allowUnfree = true; - }; - - qbotArgs = mkQBotArgs pkgs; - - inherit (qbotArgs) ruby; - - bundler = pkgs.bundler.override { inherit ruby; }; - bundix = pkgs.bundix.override { inherit bundler; }; - - qbot = pkgs.callPackage ./. qbotArgs; - }; - - # withCommon : (Dict Any -> Dict Any) -> Dict (Dict Any) - withCommon = fn: forEachSystem (system: fn (commonEnv system)); - - in { - overlays = rec { - qbot = final: prev: { - qbot = final.callPackage ./. (mkQBotArgs final); - }; - - default = qbot; - }; - - nixosModules = rec { - qbot = import nix/module.nix { inherit nixConfig; }; - default = qbot; - }; - - packages = withCommon (env: { - inherit (env) qbot; - default = env.qbot; - }); - - devShells = withCommon (env: let - shell = import ./shell.nix { - inherit (env) pkgs; - pkg = env.qbot; - }; - in { - qbot = shell; - default = shell; - }); - - apps = withCommon (env: let - inherit (env) pkgs bundler bundix; - - update-deps = pkgs.writeShellApplication { - name = "update-deps"; - - runtimeInputs = [ bundler bundix ]; - - text = '' - rm -f Gemfile.lock - - bundle lock \ - --add-platform ruby \ - --remove-platform x86_64-linux \ - || bundle lock \ - --add-platform ruby - - bundix - ''; - }; - - in { - update-deps = { - type = "app"; - program = nixpkgs.lib.getExe update-deps; - }; - }); - }; + description = "qbot flake"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable-small"; + + # TODO: remove once nixpkgs#272969 is merged + rust-overlay.url = "github:oxalica/rust-overlay"; + + gitignore.url = "github:hercules-ci/gitignore.nix"; + gitignore.inputs.nixpkgs.follows = "nixpkgs"; + }; + + nixConfig = { + extra-substituters = "https://qbot.cachix.org"; + extra-trusted-public-keys = "qbot.cachix.org-1:xkDcKYI5RucucGnOvREbPYj3+Ld1iVco0UFNQj1JVc8="; + }; + + outputs = + { + self, + nixpkgs, + rust-overlay, + gitignore, + }@flakes: + let + # forEachSystem : (Str -> Set Any) -> Set (Set Any); + forEachSystem = + let + inherit (nixpkgs.lib) genAttrs systems; + in + genAttrs systems.flakeExposed; + + mkQBotArgs = pkgs: rec { + ruby = pkgs.ruby_3_2; + + # TODO: remove once nixpkgs#272969 is merged + rustc = rust-overlay.packages.${pkgs.system}.rust; + cargo = rustc; + + inherit (gitignore.lib) gitignoreSource; + }; + + commonEnv = system: rec { + pkgs = import nixpkgs { + inherit system; + config.allowUnfree = true; + }; + + qbotArgs = mkQBotArgs pkgs; + + inherit (qbotArgs) ruby; + + bundler = pkgs.bundler.override { inherit ruby; }; + bundix = pkgs.bundix.override { inherit bundler; }; + + qbot = pkgs.callPackage ./. qbotArgs; + }; + + # withCommon : (Dict Any -> Dict Any) -> Dict (Dict Any) + withCommon = fn: forEachSystem (system: fn (commonEnv system)); + + in + { + overlays = rec { + qbot = final: prev: { + qbot = final.callPackage ./. (mkQBotArgs final); + }; + + default = qbot; + }; + + nixosModules = rec { + qbot = import nix/module.nix { inherit nixConfig; }; + default = qbot; + }; + + packages = withCommon (env: { + inherit (env) qbot; + default = env.qbot; + }); + + devShells = withCommon ( + env: + let + shell = import ./shell.nix { + inherit (env) pkgs; + pkg = env.qbot; + }; + in + { + qbot = shell; + default = shell; + } + ); + + apps = withCommon ( + env: + let + inherit (env) pkgs bundler bundix; + + update-deps = pkgs.writeShellApplication { + name = "update-deps"; + + runtimeInputs = [ + bundler + bundix + ]; + + text = '' + rm -f Gemfile.lock + + bundle lock \ + --add-platform ruby \ + --remove-platform x86_64-linux \ + || bundle lock \ + --add-platform ruby + + bundix + ''; + }; + + in + { + update-deps = { + type = "app"; + program = nixpkgs.lib.getExe update-deps; + }; + } + ); + }; } diff --git a/nix/module.nix b/nix/module.nix index 3528649..8cfb76b 100644 --- a/nix/module.nix +++ b/nix/module.nix @@ -1,90 +1,112 @@ { nixConfig, ... }: -{ lib, pkgs, config, ... }: +{ + lib, + pkgs, + config, + ... +}: let - cfg = config.services.qbot; - - configFile = pkgs.writeText "config.json" (builtins.toJSON cfg.config); - -in { - options.services.qbot = let - inherit (lib) - mkOption mkEnableOption mkPackageOption; - in { - enable = mkEnableOption "qbot service"; - - package = mkPackageOption pkgs "qbot" { }; - - config = let - inherit (lib.types) - str int enum nullOr listOf; - - mkOpt' = type: description: default: rest: - mkOption ({ inherit type description default; } // rest); - - mkOpt = t: d: mkOpt' t d null; - - in { - token = mkOpt str "Discord gateway bot token" { }; - client_id = mkOpt int "Discord application client ID" { }; - owner = mkOpt int "Discord user ID of the bot's owner" { }; - - arch = { - mirror = mkOpt' str "Mirror server hosting Arch Linux repos" - "https://mirrors.edge.kernel.org/archlinux/" { }; - - repos = mkOpt' (listOf str) "List of repositories to index" - [ "core" "extra" "multilib" ] { }; - }; - - database = { - type = mkOpt' (enum [ "sqlite3" "oracle_enhanced" ]) - "Type of database to connect to" "sqlite3" { }; - - db = mkOpt' str "Database name or path" "db.sqlite3" { }; - - user = mkOpt (nullOr str) "Database username" { }; - pass = mkOpt (nullOr str) "Database password" { }; - }; - - my_repo = mkOpt' str "The bot's VCS repository" - "https://github.com/arch-community/qbot" { }; - - modules = mkOpt (listOf str) "List of modules to load" { }; - - default_prefix = mkOpt' str "Default command prefix" "." { }; - - bot_id_allowlist = mkOpt (listOf int) - "User IDs that ignore the bot filter" { }; + cfg = config.services.qbot; + + configFile = pkgs.writeText "config.json" (builtins.toJSON cfg.config); + +in +{ + options.services.qbot = + let + inherit (lib) + mkOption + mkEnableOption + mkPackageOption + ; + in + { + enable = mkEnableOption "qbot service"; + + package = mkPackageOption pkgs "qbot" { }; + + config = + let + inherit (lib.types) + str + int + enum + nullOr + listOf + ; + + mkOpt' = + type: description: default: rest: + mkOption ({ inherit type description default; } // rest); + + mkOpt = t: d: mkOpt' t d null; + + in + { + token = mkOpt str "Discord gateway bot token" { }; + client_id = mkOpt int "Discord application client ID" { }; + owner = mkOpt int "Discord user ID of the bot's owner" { }; + + arch = { + mirror = + mkOpt' str "Mirror server hosting Arch Linux repos" "https://mirrors.edge.kernel.org/archlinux/" + { }; + + repos = mkOpt' (listOf str) "List of repositories to index" [ "core" "extra" "multilib" ] { }; + }; + + database = { + type = mkOpt' (enum [ + "sqlite3" + "oracle_enhanced" + ]) "Type of database to connect to" "sqlite3" { }; + + db = mkOpt' str "Database name or path" "db.sqlite3" { }; + + user = mkOpt (nullOr str) "Database username" { }; + pass = mkOpt (nullOr str) "Database password" { }; + }; + + my_repo = mkOpt' str "The bot's VCS repository" "https://github.com/arch-community/qbot" { }; + + modules = mkOpt (listOf str) "List of modules to load" { }; + + default_prefix = mkOpt' str "Default command prefix" "." { }; + + bot_id_allowlist = mkOpt (listOf int) "User IDs that ignore the bot filter" { }; }; }; - config = let - inherit (lib) mkIf getExe; - in mkIf cfg.enable { - systemd.services.qbot = { - description = "qbot discord bot"; - - wantedBy = [ "multi-user.target" ]; - after = [ "network.target" ]; - - serviceConfig = { - ExecStart = "${getExe cfg.package}" - + " -c ${configFile}" - + " --state-dir \${STATE_DIRECTORY}" - + " --no-console"; - - StateDirectory = "qbot"; - DynamicUser = true; - }; + config = + let + inherit (lib) mkIf getExe; + in + mkIf cfg.enable { + systemd.services.qbot = { + description = "qbot discord bot"; + + wantedBy = [ "multi-user.target" ]; + after = [ "network.target" ]; + + serviceConfig = { + ExecStart = + "${getExe cfg.package}" + + " -c ${configFile}" + + " --state-dir \${STATE_DIRECTORY}" + + " --no-console"; + + StateDirectory = "qbot"; + DynamicUser = true; }; + }; - nix.settings = { - substituters = [ nixConfig.extra-substituters ]; - trusted-public-keys = [ nixConfig.extra-trusted-public-keys ]; - }; + nix.settings = { + substituters = [ nixConfig.extra-substituters ]; + trusted-public-keys = [ nixConfig.extra-trusted-public-keys ]; + }; }; } -# vim: et ts=4 sw=4 +# vim: et ts=2 sw=2 diff --git a/shell.nix b/shell.nix index f039f11..18c4c18 100644 --- a/shell.nix +++ b/shell.nix @@ -1,16 +1,18 @@ { pkgs, pkg }: pkg.overrideAttrs (oa: { - nativeBuildInputs = oa.nativeBuildInputs ++ (with pkgs; [ - git - graphviz - loc - (sqlite.override { interactive = true; }) - yq-go - ]); + nativeBuildInputs = + oa.nativeBuildInputs + ++ (with pkgs; [ + git + graphviz + loc + (sqlite.override { interactive = true; }) + yq-go + ]); - BUNDLE_FORCE_RUBY_PLATFORM = "1"; + BUNDLE_FORCE_RUBY_PLATFORM = "1"; - LD_LIBRARY_PATH = oa.passthru.libPath; - FONTCONFIG_FILE = oa.passthru.fontconfigFile; + LD_LIBRARY_PATH = oa.passthru.libPath; + FONTCONFIG_FILE = oa.passthru.fontconfigFile; })