Skip to content

Commit

Permalink
Support connecting via MySQL Unix socket
Browse files Browse the repository at this point in the history
  • Loading branch information
Brawl345 committed Sep 10, 2024
1 parent 0dd619b commit 6207068
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 55 deletions.
74 changes: 35 additions & 39 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,20 @@
nixpkgs.url = "github:nixos/nixpkgs?ref=nixpkgs-unstable";
};

outputs = { self, nixpkgs, ... }:
outputs =
{ self, nixpkgs, ... }:

let
forAllSystems = function:
forAllSystems =
function:
nixpkgs.lib.genAttrs [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
]
(system: function nixpkgs.legacyPackages.${system});
] (system: function nixpkgs.legacyPackages.${system});

version =
if (self ? shortRev)
then self.shortRev
else "dev";
version = if (self ? shortRev) then self.shortRev else "dev";
in
{

Expand All @@ -32,41 +30,39 @@
rssbot = self.packages.${prev.system}.default;
};

devShells = forAllSystems
(pkgs: {
default = pkgs.mkShell {
packages = [
pkgs.go
pkgs.golangci-lint
];
};
});

devShells = forAllSystems (pkgs: {
default = pkgs.mkShell {
packages = [
pkgs.go
pkgs.golangci-lint
];
};
});

packages = forAllSystems
(pkgs: {
rssbot =
pkgs.buildGoModule
{
pname = "rssbot";
inherit version;
src = pkgs.lib.cleanSource self;
packages = forAllSystems (pkgs: {
rssbot = pkgs.buildGoModule {
pname = "rssbot";
inherit version;
src = pkgs.lib.cleanSource self;

# Update the hash if go dependencies change!
# vendorHash = pkgs.lib.fakeHash;
vendorHash = "sha256-mo30V7ISVFY8Rl3yXChP6pbehV9hTPH3UlBLDb1dzNE=";
# Update the hash if go dependencies change!
# vendorHash = pkgs.lib.fakeHash;
vendorHash = "sha256-mo30V7ISVFY8Rl3yXChP6pbehV9hTPH3UlBLDb1dzNE=";

ldflags = [ "-s" "-w" ];
ldflags = [
"-s"
"-w"
];

meta = {
description = "RSS bot for Telegram";
homepage = "https://github.com/Brawl345/rssbot";
license = pkgs.lib.licenses.unlicense;
platforms = pkgs.lib.platforms.darwin ++ pkgs.lib.platforms.linux;
};
};
meta = {
description = "RSS bot for Telegram";
homepage = "https://github.com/Brawl345/rssbot";
license = pkgs.lib.licenses.unlicense;
platforms = pkgs.lib.platforms.darwin ++ pkgs.lib.platforms.linux;
};
};

default = self.packages.${pkgs.system}.rssbot;
});
default = self.packages.${pkgs.system}.rssbot;
});
};
}
69 changes: 63 additions & 6 deletions module.nix
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
{ config, lib, pkgs, ... }:
{
config,
lib,
pkgs,
...
}:

let
cfg = config.services.rssbot;
defaultUser = "rssbot";
inherit (lib) mkEnableOption mkPackageOption mkOption mkIf types optionalAttrs;
inherit (lib)
mkEnableOption
mkPackageOption
mkOption
mkIf
types
optional
optionalAttrs
optionalString
;
in
{
options.services.rssbot = {
Expand Down Expand Up @@ -60,31 +74,73 @@ in
};

passwordFile = lib.mkOption {
type = types.path;
type = types.nullOr types.path;
default = null;
description = "Database user password file.";
};

socket = mkOption {
type = types.nullOr types.path;
default =
if config.services.rssbot.database.passwordFile == null then "/run/mysqld/mysqld.sock" else null;
example = "/run/mysqld/mysqld.sock";
description = "Path to the unix socket file to use for authentication.";
};

createLocally = mkOption {
type = types.bool;
default = true;
description = "Create the database locally";
};
};

};

config = mkIf cfg.enable {

assertions = [
{
assertion = !(cfg.database.socket != null && cfg.database.passwordFile != null);
message = "Only one of services.rssbot.database.socket or services.rssbot.database.passwordFile can be set.";
}
{
assertion = cfg.database.socket != null || cfg.database.passwordFile != null;
message = "Either services.rssbot.database.socket or services.rssbot.database.passwordFile must be set.";
}
];

services.mysql = lib.mkIf cfg.database.createLocally {
enable = lib.mkDefault true;
package = lib.mkDefault pkgs.mariadb;
ensureDatabases = [ cfg.database.name ];
ensureUsers = [
{
name = cfg.database.user;
ensurePermissions = {
"${cfg.database.name}.*" = "ALL PRIVILEGES";
};
}
];
};

systemd.services.rssbot = {
description = "RSS Bot for Telegram";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];

script = ''
export BOT_TOKEN="$(< $CREDENTIALS_DIRECTORY/BOT_TOKEN )"
export MYSQL_PASSWORD="$(< $CREDENTIALS_DIRECTORY/MYSQL_PASSWORD )"
${optionalString (cfg.database.passwordFile != null) ''
export MYSQL_PASSWORD="$(< $CREDENTIALS_DIRECTORY/MYSQL_PASSWORD )"
''}
exec ${cfg.package}/bin/rssbot
'';

serviceConfig = {
LoadCredential = [
"BOT_TOKEN:${cfg.botTokenFile}"
"MYSQL_PASSWORD:${cfg.database.passwordFile}"
];
] ++ optional (cfg.database.passwordFile != null) "MYSQL_PASSWORD:${cfg.database.passwordFile}";

Restart = "always";
User = cfg.user;
Expand All @@ -97,6 +153,7 @@ in
MYSQL_PORT = toString cfg.database.port;
MYSQL_USER = cfg.database.user;
MYSQL_DB = cfg.database.name;
MYSQL_SOCKET = cfg.database.socket;
};
};

Expand Down
34 changes: 24 additions & 10 deletions storage/storage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package storage

import (
"cmp"
"embed"
"fmt"
"os"
Expand All @@ -25,16 +26,29 @@ func Connect() (*DB, error) {
port := strings.TrimSpace(os.Getenv("MYSQL_PORT"))
user := strings.TrimSpace(os.Getenv("MYSQL_USER"))
password := strings.TrimSpace(os.Getenv("MYSQL_PASSWORD"))
db := strings.TrimSpace(os.Getenv("MYSQL_DB"))

connectionString := fmt.Sprintf(
"%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
user,
password,
host,
port,
db,
)
dbname := strings.TrimSpace(os.Getenv("MYSQL_DB"))
tls := cmp.Or(strings.TrimSpace(os.Getenv("MYSQL_TLS")), "false")
socket := strings.TrimSpace(os.Getenv("MYSQL_SOCKET"))

var connectionString string
if socket != "" {
connectionString = fmt.Sprintf(
"%s@unix(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
user,
socket,
dbname,
)
} else {
connectionString = fmt.Sprintf(
"%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local&tls=%s",
user,
password,
host,
port,
dbname,
tls,
)
}

conn, err := sqlx.Connect("mysql", connectionString)
if err != nil {
Expand Down

0 comments on commit 6207068

Please sign in to comment.