w4 / bin

:wastebasket: a paste bin.
Other
416 stars 44 forks source link

NixOS Module #22

Open j0hax opened 2 years ago

j0hax commented 2 years ago

Considering there's already a Nix example in the README, would it be possible to have an official derivation for Nixpkgs and a module to enable it as a service?

I would love to have this up and running on my NixOS-based server!

Cheers

w4 commented 2 years ago

Hi @j0hax, thanks for the suggestion. It would definitely be nice to add a derivation to Nixpkgs. I’ll certainly do that at some point in the near future. For now, you can use the same configuration as is running on https://bin.gy/

{ config, pkgs, lib, ... }:
let
  bin = pkgs.rustPlatform.buildRustPackage rec {
    pname = "bin";
    version = "2.0.0";

    src = pkgs.fetchFromGitHub {
      owner = "w4";
      repo = "bin";
      rev = "057ed640ee503469fe4c3b0c164aa1b1bffc6cc6";
      sha256 = "1g67k9m4aflhf2sb2xggh896xgp9vh7n2z0w5kxql0p3xa0shsi9";
    };
    cargoSha256 = "1v2ks2vdr5knn6kri0d03rvzx1mkv5qhv7xphsxpwfyykhbc0j7h";

    LIBCLANG_PATH="${pkgs.llvmPackages.libclang}/lib";
  };
in {
  containers.bin = {
    autoStart = true;
    ephemeral = true;

    macvlans = [ "vlan101" ];

    config = { config, pkgs, ... }:
      {
        networking = {
          interfaces.mv-vlan101.ipv4.addresses = [ { address = "10.0.64.106"; prefixLength = 24; } ];
          defaultGateway = "10.0.64.2";
          nameservers = [ "10.0.64.1" ];
          firewall.allowedTCPPorts = [ 8080 ];
        };

        systemd.services.bin = {
          enable = true;
          unitConfig = {
            After = "network-online.target";
            Wants = "network-online.target systemd-networkd-wait-online.service";
          };
          serviceConfig = {
            Type = "exec";
            ExecStart = "${bin}/bin/bin 0.0.0.0:8080";
            Restart = "on-failure";
          };
          wantedBy = ["default.target"];
        };
      };
  };
}
w4 commented 2 years ago

Hi @j0hax, MR is open at https://github.com/NixOS/nixpkgs/pull/165488 - hopefully it'll be merged soon

j0hax commented 2 years ago

Excellent, thanks!

Alistair1231 commented 1 year ago

Any updates on this? There was a review on Apr 4.

w4 commented 1 year ago

Hi, there's a flake included instead now:

{
  inputs.paste-bin = {
    url = "github:w4/bin";
    inputs.nixpkgs.follows = "nixpkgs";
  };

  outputs = { ... }:
    {
      nixosConfigurations.myBox = nixpkgs.lib.nixosSystem {
        modules = [
          paste-bin.nixosModules.x86_64-linux.default
          {
            services.paste-bin = {
              enable = true;
              bindAddress = "[::]:8080";
            };
          }
        ];
      };
    }
}
Alistair1231 commented 1 year ago

Thanks for the quick response. I'm new to nix and haven't used flakes before. Normally I would run something like this to start e.g. Firefox nix-shell -p firefox --run firefox

From what I understand, with flakes, I would want to add the experimental flags to my configs add experimental-features = nix-command flakes to /etc/nix/nix.conf

Afterwards run it like so: nix shell github:w4/bin --command bin
--run doesn't seem to exist for nix shell so I need --command instead.

Are there any differences apart from how I run it?

Also, is there a reason why this isn't simply added to the package repository of nix?

w4 commented 1 year ago

No problem. If you're just using Nix imperatively you can use nix run github:w4/bin. I'd highly recommend checking out declarative flakes as it's the New Way(tm).

It's a little bit better for devs to just include flakes with their packages along with cachix so you're always up to date and not waiting on Hydra whenever a new version is pushed.

Alistair1231 commented 1 year ago

Oh, I see. Thanks. I followed the instructions on their website, but that must refer to the "stable" and not the "experimental" approach.

The settings for the flake do not seem to work. I can pass the arguments to the program like this:

$ nix run github:w4/bin -- '0.0.0.0:8821'
 INFO  bin > Listening on http://0.0.0.0:8821
 INFO  actix_server::builder > Starting 4 workers

But it complains when I try to use the option from the flake: The settings from the flake:

options.services.paste-bin = {
              enable = mkEnableOption "paste-bin";
              bindAddress = mkOption {
                default = "[::]:8000";
                description = "Address and port to listen on";
                type = types.str;
              };
$ nix run github:w4/bin --arg bindAddress '0.0.0.0:8821'
error: syntax error, unexpected ':', expecting end of file

       at «string»:1:8:

            1| 0.0.0.0:
             |        ^

Am I missing something?

Also, it might help some people if this were shown in the README.md. Simply something like this as an example would be a great starting off point for people unfamiliar with nix I think: nix run github:w4/bin -- '0.0.0.0:8821

But I agree allowing for devs to include their own flake definition in their repos, like with Dockerfiles makes it much easier to support for the dev. Great addition.

w4 commented 1 year ago

That module is only for declarative usage of the service through your configuration.nix unfortunately. You'll have to pass the arguments manually like they're substituted into the systemd execstart

nix run github:w4/bin -- --buffer-size 1000 --max-paste-size 1024 0.0.0.0:8821
Alistair1231 commented 1 year ago

I see. Thanks.