From 7f51f84066bcfbd5a963ced730861dd16b539294 Mon Sep 17 00:00:00 2001 From: notohh Date: Tue, 14 May 2024 22:31:32 -0400 Subject: [PATCH] init commit --- .envrc | 1 + .gitignore | 3 + Cargo.toml | 11 +++ commands/Cargo.toml | 6 ++ commands/src/main.rs | 3 + flake.lock | 222 +++++++++++++++++++++++++++++++++++++++++++ flake.nix | 80 ++++++++++++++++ helix/Cargo.toml | 6 ++ helix/src/main.rs | 3 + nix/module.nix | 30 ++++++ src/main.rs | 43 +++++++++ statix.toml | 3 + 12 files changed, 411 insertions(+) create mode 100644 .envrc create mode 100644 Cargo.toml create mode 100644 commands/Cargo.toml create mode 100644 commands/src/main.rs create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 helix/Cargo.toml create mode 100644 helix/src/main.rs create mode 100644 nix/module.nix create mode 100644 src/main.rs create mode 100644 statix.toml diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore index 3ca43ae..51afe91 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,7 @@ Cargo.lock # MSVC Windows builds of rustc generate these, which store debugging information *.pdb +result/ +.env +.pre-commit-config.yaml diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..5ed24f0 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "botoh" +version = "0.1.0" +edition = "2021" + +[dependencies] +dotenv = "0.15.0" +pretty_env_logger = "0.5.0" +tokio = {version = "1.37.0", features = ["full"]} +twitch-irc = "5.0.1" +twitch_api = "0.7.0-rc.7" diff --git a/commands/Cargo.toml b/commands/Cargo.toml new file mode 100644 index 0000000..246cd80 --- /dev/null +++ b/commands/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "commands" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/commands/src/main.rs b/commands/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/commands/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..f56edaf --- /dev/null +++ b/flake.lock @@ -0,0 +1,222 @@ +{ + "nodes": { + "fenix": { + "inputs": { + "nixpkgs": "nixpkgs", + "rust-analyzer-src": "rust-analyzer-src" + }, + "locked": { + "lastModified": 1714544767, + "narHash": "sha256-kF1bX+YFMedf1g0PAJYwGUkzh22JmULtj8Rm4IXAQKs=", + "owner": "nix-community", + "repo": "fenix", + "rev": "73124e1356bde9411b163d636b39fe4804b7ca45", + "type": "github" + }, + "original": { + "owner": "nix-community", + "ref": "monthly", + "repo": "fenix", + "type": "github" + } + }, + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1696426674, + "narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "0f9255e01c2351cc7d116c072cb317785dd33b33", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "gitignore": { + "inputs": { + "nixpkgs": [ + "pre-commit-hooks", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1709087332, + "narHash": "sha256-HG2cCnktfHsKV0s4XW83gU3F57gaTljL9KNSuG6bnQs=", + "owner": "hercules-ci", + "repo": "gitignore.nix", + "rev": "637db329424fd7e46cf4185293b9cc8c88c95394", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "gitignore.nix", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1714253743, + "narHash": "sha256-mdTQw2XlariysyScCv2tTE45QSU9v/ezLcHJ22f0Nxc=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "58a1abdbae3217ca6b702f03d3b35125d88a2994", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-stable": { + "locked": { + "lastModified": 1710695816, + "narHash": "sha256-3Eh7fhEID17pv9ZxrPwCLfqXnYP006RKzSs0JptsN84=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "614b4613980a522ba49f0d194531beddbb7220d3", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-23.11", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1715653339, + "narHash": "sha256-7lR9tpVXviSccl07GXI0+ve/natd24HAkuy1sQp0OlI=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "abd6d48f8c77bea7dc51beb2adfa6ed3950d2585", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_3": { + "locked": { + "lastModified": 1710765496, + "narHash": "sha256-p7ryWEeQfMwTB6E0wIUd5V2cFTgq+DRRBz2hYGnJZyA=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "e367f7a1fb93137af22a3908f00b9a35e2d286a7", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "pre-commit-hooks": { + "inputs": { + "flake-compat": "flake-compat", + "flake-utils": "flake-utils", + "gitignore": "gitignore", + "nixpkgs": "nixpkgs_3", + "nixpkgs-stable": "nixpkgs-stable" + }, + "locked": { + "lastModified": 1715609711, + "narHash": "sha256-/5u29K0c+4jyQ8x7dUIEUWlz2BoTSZWUP2quPwFCE7M=", + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "rev": "c182c876690380f8d3b9557c4609472ebfa1b141", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "type": "github" + } + }, + "root": { + "inputs": { + "fenix": "fenix", + "nixpkgs": "nixpkgs_2", + "pre-commit-hooks": "pre-commit-hooks", + "systems": "systems_2" + } + }, + "rust-analyzer-src": { + "flake": false, + "locked": { + "lastModified": 1714501997, + "narHash": "sha256-g31zfxwUFzkPgX0Q8sZLcrqGmOxwjEZ/iqJjNx4fEGo=", + "owner": "rust-lang", + "repo": "rust-analyzer", + "rev": "49e502b277a8126a9ad10c802d1aaa3ef1a280ef", + "type": "github" + }, + "original": { + "owner": "rust-lang", + "ref": "nightly", + "repo": "rust-analyzer", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "systems_2": { + "locked": { + "lastModified": 1689347949, + "narHash": "sha256-12tWmuL2zgBgZkdoB6qXZsgJEH9LR3oUgpaQq2RbI80=", + "owner": "nix-systems", + "repo": "default-linux", + "rev": "31732fcf5e8fea42e59c2488ad31a0e651500f68", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default-linux", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..cf08a97 --- /dev/null +++ b/flake.nix @@ -0,0 +1,80 @@ +{ + inputs = { + systems.url = "github:nix-systems/default-linux"; + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + fenix.url = "github:nix-community/fenix/monthly"; + pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix"; + }; + + outputs = { + self, + systems, + nixpkgs, + fenix, + pre-commit-hooks, + }: let + eachSystem = nixpkgs.lib.genAttrs (import systems); + pkgsFor = eachSystem (system: + import nixpkgs { + localSystem.system = system; + overlays = [fenix.overlays.default]; + }); + in { + packages = eachSystem (system: let + pkgs = nixpkgs.legacyPackages.${system}; + inherit ((builtins.fromTOML (builtins.readFile ./Cargo.toml)).package) version; + in { + default = pkgsFor.${system}.rustPlatform.buildRustPackage { + pname = "botoh"; + version = "${version}"; + + src = self; + + cargoLock = { + lockFile = ./.; + }; + + nativeBuildInputs = with pkgs; [pkg-config]; + buildInputs = with pkgs; [openssl]; + + doCheck = false; + }; + }); + checks = eachSystem (system: { + pre-commit-check = pre-commit-hooks.lib.${system}.run { + src = ./.; + hooks = { + # rust + rustfmt.enable = true; + clippy.enable = true; + # nix + statix.enable = true; + alejandra.enable = true; + deadnix.enable = true; + }; + }; + }); + devShells = eachSystem (system: { + default = pkgsFor.${system}.mkShell { + inherit (self.checks.${system}.pre-commit-check) shellHook; + packages = with pkgsFor.${system}; [ + nil + alejandra + openssl + pkg-config + (fenix.packages.${system}.complete.withComponents [ + "cargo" + "clippy" + "rust-src" + "rustc" + "rustfmt" + "rust-analyzer" + ]) + ]; + RUST_BACKTRACE = 1; + RUST_SRC_PATH = "${fenix.packages.${system}.complete.rust-src}/lib/rustlib/src/rust/library"; + }; + }); + nixosModules.default = import ./nix/module.nix {inherit self;}; + }; +} diff --git a/helix/Cargo.toml b/helix/Cargo.toml new file mode 100644 index 0000000..e70e9e2 --- /dev/null +++ b/helix/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "helix" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/helix/src/main.rs b/helix/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/helix/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/nix/module.nix b/nix/module.nix new file mode 100644 index 0000000..7966a1e --- /dev/null +++ b/nix/module.nix @@ -0,0 +1,30 @@ +{self}: { + pkgs, + config, + lib, + ... +}: let + inherit (lib) types; + inherit (lib.modules) mkIf; + inherit (lib.options) mkOption mkEnableOption; + inherit (pkgs.stdenv.hostPlatform) system; + cfg = config.services.forcebot_rs; +in { + options.services.botoh = { + enable = mkEnableOption '' + Enable botoh + ''; + + package = mkOption { + type = types.package; + inherit (self.packages.${system}) default; + }; + }; + + config = mkIf cfg.enable { + systemd.services.forcebot_rs = { + wantedBy = ["multi-user.target"]; + serviceConfig.ExecStart = "${cfg.package}/bin/botoh"; + }; + }; +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..3b8d07c --- /dev/null +++ b/src/main.rs @@ -0,0 +1,43 @@ +use dotenv::dotenv; +use std::env; +use twitch_irc::login::StaticLoginCredentials; +use twitch_irc::ClientConfig; +use twitch_irc::SecureTCPTransport; +use twitch_irc::TwitchIRCClient; + +#[tokio::main] +pub async fn main() { + dotenv().ok(); + let twitch_id = match env::var("TWITCH_ID").to_owned() { + Ok(val) => val, + Err(_) => { + eprintln!("TWITCH_ID not found in environment variables."); + return; + } + }; + + let twitch_oauth = match env::var("TWITCH_OAUTH").to_owned() { + Ok(val) => val, + Err(_) => { + eprintln!("TWITCH_OAUTH not found in environment variables."); + return; + } + }; + + let config = + ClientConfig::new_simple(StaticLoginCredentials::new(twitch_id, Some(twitch_oauth))); + let (mut incoming_messages, client) = + TwitchIRCClient::::new(config); + + let join_handle = + tokio::spawn(async move { while let Some(message) = incoming_messages.recv().await {} }); + + client.join("notnotoh".to_owned()).unwrap(); + + client + .say("notnotoh".to_owned(), "test".to_owned()) + .await + .unwrap(); + + join_handle.await.unwrap(); +} diff --git a/statix.toml b/statix.toml new file mode 100644 index 0000000..fbe25a9 --- /dev/null +++ b/statix.toml @@ -0,0 +1,3 @@ +disabled = [] +nix_version = '2.4' +ignore = ['.direnv']