# Edit this configuration file to define what should be installed on
# your system.  Help is available in the configuration.nix(5) man page
# and in the NixOS manual (accessible by running ‘nixos-help’).

{ config, pkgs, inputs, ... }:

{
  imports =
    [
      ./hardware-configuration.nix
      inputs.home-manager.nixosModules.default
    ];

  nix.settings.experimental-features = [ "nix-command" "flakes" ];
    
  # Bootloader.
  boot.loader.systemd-boot.enable = true;
  boot.loader.efi.canTouchEfiVariables = true;

  networking.hostName = "arabasta"; # Define your hostname.
  # networking.wireless.enable = true;  # Enables wireless support via wpa_supplicant.

  # Configure network proxy if necessary
  # networking.proxy.default = "http://user:password@proxy:port/";
  # networking.proxy.noProxy = "127.0.0.1,localhost,internal.domain";

  # Enable networking
  networking.networkmanager.enable = true;

  # Set your time zone.
  time.timeZone = "Europe/Oslo";

  # Select internationalisation properties.
  i18n.defaultLocale = "en_US.UTF-8";

  # Configure keymap in X11
  services.xserver.xkb = {
    layout = "no";
    variant = "";
  };

  # Configure console keymap
  console.keyMap = "no";

  # Define a user account. Don't forget to set a password with ‘passwd’.
  users.users.sid = {
    isNormalUser = true;
    description = "sid";
    extraGroups = [ "networkmanager" "wheel" "docker" ];
    packages = with pkgs; [];
  };

  # enable docker
  virtualisation.docker.enable = true;

  # use docker without Root access (Rootless docker)
  virtualisation.docker.rootless = {
    enable = true;
    setSocketVariable = true;
  };
  
 	# Home Manager
	home-manager = {
		extraSpecialArgs = { inherit inputs; };
		users = {
			"sid" = import ./home.nix;
		};
	};

  # Allow unfree packages
  nixpkgs.config.allowUnfree = true;

  # List packages installed in system profile. To search, run:
  # $ nix search wget
  environment.systemPackages = with pkgs; [
    vim
    git
    tailscale
  ];

  services = {
    tailscale.enable = true;
    paperless = {
      enable = true;
      dataDir = "/mnt/media/paperless";
      mediaDir = "/mnt/media/paperless/media";
      address = "0.0.0.0";
      port = 28981;
    };
    navidrome = {
      enable = true;
      settings = {
        Address = "0.0.0.0";
        Port = 4533;
        MusicFolder = "/mnt/media/navidrome-music";
        EnableTranscodingConfig = "true";
        DefaultTheme = "Catppuccin Macchiato";
      };
    };
    forgejo = {
      enable = true;
      database.type = "postgres";
      lfs.enable = true;
      settings = {
        server = {
          APP_NAME = "forgejo";
          APP_SLOGAN = "Ah well, it's just my programming.";
          RUN_MODE = "prod";
          SSH_DOMAIN = "code.kjelsrud.dev";
          DOMAIN = "code.kjelsrud.dev";
          HTTP_PORT = 3000;
          HTTP_ADDR = "0.0.0.0";
          ROOT_URL = "https://code.kjelsrud.dev";
          DISABLE_SSH = true;
          LFS_START_SERVER = true;
          LFS_JWT_SECRET = "CHANGEME";
          OFFLINE_MODE = true;
        };
        session = {
          COOKIE_SECURE = true;
          PROVIDER = "file";
        };
        security = {
          INSTALL_LOCK = true;
          PASSWORD_HASH_ALGO = "pbkdf2_hi";
          INTERNAL_TOKEN = "CHANGEME";
        };
        service = {
          DISABLE_REGISTRATION = true;
          REQUIRE_SIGNIN_VIEW = false;
          REGISTER_EMAIL_CONFIRM = false;
          ENABLE_NOTIFY_MAIL = false;
          ALLOW_ONLY_EXTERNAL_REGISTRATION = false;
          ENABLE_CAPTCHA = false;
          DEFAULT_KEEP_EMAIL_PRIVATE = false;
          DEFAULT_ALLOW_CREATE_ORGANIZATION = false;
          DEFAULT_ENABLE_TIMETRACKING = true;
          NO_REPLY_ADDRESS = "noreply.localhost";
        };
        mailer.ENABLED = false;
        openid = {
          ENABLE_OPENID_SIGNIN = false;
          ENABLE_OPENID_SIGNUP = false;
        };
        actions = {
          ENABLED = true;
          DEFAULT_ACTIONS_URL = "https://code.forgejo.org";
        };
        oauth2.JWT_SECRET = "CHANGEME";
      };
    };
  };

	fileSystems."/mnt/media" = {
    device = "/dev/disk/by-uuid/956ec2bb-357c-4959-917f-ece881c4615c";
    fsType = "ext4";
    options = [ "defaults" "nofail" ];
  };
  
  # Some programs need SUID wrappers, can be configured further or are
  # started in user sessions.
  # programs.mtr.enable = true;
  # programs.gnupg.agent = {
  #   enable = true;
  #   enableSSHSupport = true;
  # };

  # List services that you want to enable:

  # Enable the OpenSSH daemon.
  services.openssh.enable = true;

  # Open ports in the firewall.
  networking.firewall.allowedTCPPorts = [ 37923 ];
  # networking.firewall.allowedUDPPorts = [ ... ];
  # Or disable the firewall altogether.
  # networking.firewall.enable = false;

  # This value determines the NixOS release from which the default
  # settings for stateful data, like file locations and database versions
  # on your system were taken. It‘s perfectly fine and recommended to leave
  # this value at the release version of the first install of this system.
  # Before changing this value read the documentation for this option
  # (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
  system.stateVersion = "25.05"; # Did you read the comment?
}
