I set up a VPS on Linode running NixOS using this guide a few years ago: https://www.linode.com/docs/guides/install-nixos-on-linode
I have caddy set up to serve up a few different websites and applications and it’s been working great. Recently I went to set up gotosocial and a static site, so I updated the virtualHosts under services.caddy and issued sudo nixos-rebuild switch, which finished without errors.
However, after this, the file at /etc/caddy/caddy_config was not updated with my changes to the nixos configuration, and the subdomains I’d added to virtualhosts were not accessible. I confirmed this file is being used by caddy by looking at systemctl status caddy). It’s entirely possible that I missed a step somewhere because I didn’t take thorough enough notes on how I set things up initially.
Any tips on how to figure out what’s going on here?
Have you rebooted the server or restarted the caddy.service?
I would think the service is supposed to be restarted automatically with a nixos-rebuild switch , but maybe systemd missed it that time.
Judging by the output of systemctl status caddy, rebuilding the config isn’t restarting the systemd service. I haven’t been rebooting every time, but I did give that a shot as well, and didn’t see the caddy config update either. Honestly I’d be happy just if rebuilding applied the virtualHosts changes, but that seems odd.
Huh, this sounds more like a configuration issue. Are you confident the settings are being imported (if they are in a module for example).
You could try using the following commands to troubleshoot caddy setup from the eval stage (these assume flakes, but should be possible to adapt as needed):
To see the host names:
nix eval ".#nixosConfigurations.<HOST>.config.services.caddy.virtualHosts" --apply builtins.attrNames
To see the config file itself:
nix eval --raw ".#nixosConfigurations.<HOST>.config.services.caddy.configFile" --apply builtins.readFile
Agreed with daylinmorgan on the eval-side checks. Also try sharing your (nixos) config, and maybe the outputs of:
$ readlink /etc/caddy/caddy_config
$ readlink /etc/static/caddy/caddy_config
If you’re not seeing that symlink chain point into the nix store that’s a problem, and if you’re not seeing the store location change on rebuild that’s a different problem. You could also use something like nix-tree and search for Caddyfile to see what your current system actually thinks it’s pointing to.
the first readlink output points to the second path, and the second readlink output points to /nix/store/ax6v9dx9w9wc0fz6d45gnmscwljcd02i-Caddyfile-formatted/Caddyfile
The nix eval commands (I had to append --extra-experimental-features nix-command) print:
path '/home/limited' does not contain a 'flake.nix', searching up
error: could not find a flake.nix file
here is my configuration.nix (from etc/nixos/configuration.nix with user and base domain redacted. The first 3 virtualHosts do work and are reflected in the caddy_config, but the last two do not and are not.
# Edit this configuration file to define what should be installed on
# your system. Help is available in the configuration.nix(5) man page, on
# https://search.nixos.org/options and in the NixOS manual (`nixos-help`).
{ config, lib, pkgs, ... }:
{
imports =
[ # Include the results of the hardware scan.
./hardware-configuration.nix
];
# Use the GRUB 2 boot loader.
boot.loader.grub.enable = true;
# boot.loader.grub.efiSupport = true;
# boot.loader.grub.efiInstallAsRemovable = true;
# boot.loader.efi.efiSysMountPoint = "/boot/efi";
# Define on which hard drive you want to install Grub.
# boot.loader.grub.device = "/dev/sda"; # or "nodev" for efi only
# networking.hostName = "grundrisse"; # Define your hostname.
# Pick only one of the below networking options.
# networking.wireless.enable = true; # Enables wireless support via wpa_supplicant.
# networking.networkmanager.enable = true; # Easiest to use and most distros use this by default.
networking.usePredictableInterfaceNames = false;
networking.useDHCP = false;
networking.interfaces.eth0.useDHCP = true;
# Set your time zone.
# time.timeZone = "Europe/Amsterdam";
# Configure network proxy if necessary
# networking.proxy.default = "http://user:password@proxy:port/";
# networking.proxy.noProxy = "127.0.0.1,localhost,internal.domain";
# Select internationalisation properties.
# i18n.defaultLocale = "en_US.UTF-8";
# console = {
# font = "Lat2-Terminus16";
# keyMap = "us";
# useXkbConfig = true; # use xkb.options in tty.
# };
# Enable the X11 windowing system.
# services.xserver.enable = true;
# Configure keymap in X11
# services.xserver.xkb.layout = "us";
# services.xserver.xkb.options = "eurosign:e,caps:escape";
# Enable CUPS to print documents.
# services.printing.enable = true;
# Enable sound.
# hardware.pulseaudio.enable = true;
# OR
# services.pipewire = {
# enable = true;
# pulse.enable = true;
# };
# Enable touchpad support (enabled default in most desktopManager).
# services.libinput.enable = true;
# Define a user account. Don't forget to set a password with ‘passwd’.
users.users.<user> = {
isNormalUser = true;
home = "/home/limited";
description = "nonroot user";
extraGroups = [ "wheel" "networkmanager" ]; # Enable ‘sudo’ for the user.
openssh.authorizedKeys.keys = [ "ssh-ed25519 <key> <user>" ];
packages = with pkgs; [
tree
];
linger = true;
};
# programs.firefox.enable = true;
# List packages installed in system profile. To search, run:
# $ nix search wget
environment.systemPackages = with pkgs; [
vim # Do not forget to add an editor to edit configuration.nix! The Nano editor is also installed by default.
wget
inetutils
mtr
sysstat
dive # look into docker image layers
podman-tui # status of containers in the terminal
podman-compose # start group of containers for dev
atuin
helix
];
# 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;
settings.PermitRootLogin = "no";
};
services.caddy = {
enable = true;
virtualHosts."<base domain>".extraConfig = ''
respond "Hello, world!"
'';
virtualHosts."search.<base domain>".extraConfig = ''
reverse_proxy localhost:8081
'';
virtualHosts."links.<base domain>".extraConfig = ''
reverse_proxy localhost:9090
'';
virtualHosts."social.<base domain>".extraConfig = ''
reverse_proxy localhost:8082
'';
virtualHosts."static.<base domain>".extraConfig = ''
root * /var/www/static
file_server
'';
};
networking.firewall.allowedTCPPorts = [ 80 443 ];
services.searx = {
enable = false;
redisCreateLocally = true;
environmentFile = "/home/limited/.searxng.env";
settings.server = {
bind_address = "::1";
port = 8081;
# WARNING: setting secret_key here might expose it to the nix cache
# see below for the sops or environment file instructions to prevent this
# secret_key = "Your secret key.";
};
};
services.gotosocial = {
enable = true;
setupPostgresqlDB = true;
settings = {
application-name = "GoToSocial";
host = "social.<base domain>.net";
protocol = "https";
bind-address = "127.0.0.1";
port = 8082;
};
};
# Enable common container config files in /etc/containers
virtualisation.containers.enable = true;
virtualisation = {
podman = {
enable = true;
# Create a `docker` alias for podman, to use it as a drop-in replacement
dockerCompat = true;
# Required for containers under podman-compose to be able to talk to each other.
defaultNetwork.settings.dns_enabled = true;
};
};
# Open ports in the firewall.
# networking.firewall.allowedTCPPorts = [ ... ];
# networking.firewall.allowedUDPPorts = [ ... ];
# Or disable the firewall altogether.
# networking.firewall.enable = false;
# Copy the NixOS configuration file and link it from the resulting system
# (/run/current-system/configuration.nix). This is useful in case you
# accidentally delete configuration.nix.
# system.copySystemConfiguration = true;
# This option defines the first version of NixOS you have installed on this particular machine,
# and is used to maintain compatibility with application data (e.g. databases) created on older NixOS versions.
#
# Most users should NEVER change this value after the initial install, for any reason,
# even if you've upgraded your system to a new NixOS release.
#
# This value does NOT affect the Nixpkgs version your packages and OS are pulled from,
# so changing it will NOT upgrade your system - see https://nixos.org/manual/nixos/stable/#sec-upgrading for how
# to actually do that.
#
# This value being lower than the current NixOS release does NOT mean your system is
# out of date, out of support, or vulnerable.
#
# Do NOT change this value unless you have manually inspected all the changes it would make to your configuration,
# and migrated your data accordingly.
#
# For more information, see `man configuration.nix` or https://nixos.org/manual/nixos/stable/options#opt-system.stateVersion .
system.stateVersion = "24.11"; # Did you read the comment?
}
Does the hash in the path pointed to by this link ( readlink /etc/static/caddy/caddy_config) change when you make significant modifications to services.caddy’s vhosts and rebuild? As an obvious test you could try temporarily removing one of the working vhost entries to make sure it goes away and the file changes accordingly. It should be ax6...02i before, something different after.
thanks.. I removed one of the virtualHosts entries and rebuilt and the hash didn’t change.
From this it sounds like no changes to your config whatsoever are being reflected after a rebuild. I assume your new gotosocial service isn’t running either (you can do systemctl status gotosocial to verify). You can probably mash the keyboard to put some invalid nix on any line of your config and still have the rebuild succeed.
Does that sound right? If so, there’s some problem with the way you’re writing your config or rebuilding, not an issue with your configuration content or the details of caddy itself.
I would double-check that running cat /etc/nixos/configuration.nix from the same prompt where you’re running nixos-rebuild shows the same configuration contents you shared above. Then, double-check the command you’re running to rebuild. Is it just sudo nixos-rebuild switch?
If all that checks out and there’s not a mix-up in the hosts you’re doing different things on, maybe you have env vars telling nixos-rebuild to look somewhere else for your config. I’d check echo $NIX_PATH and echo $NIXOS_CONFIG from the rebuild prompt as well.
Actually no, I messed up and forgot a semicolon on the latest virtualHost entry and it threw a bunch of errors when rebuilding. All of the commands I’ve run were run in the same SSH session so I’m confident that I’m not mixing up hosts.
Correct
echo $NIX_PATH prints /home/limited/.nix-defexpr/channels:nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos:nixos-config=/etc/nixos/configuration.nix:/nix/var/nix/profiles/per-user/root/channels
echo $NIXOS_CONFIG prints nothing.
Interesting, so you’re rebuilding against the correct config file but not seeing some or all changes.
Is systemctl status gotosocial showing a running service like I asked about above, or does it error out with no such unit or similar?
If no new services, next on my list would be making sure that your current system is actually changing by looking at readlink /run/current-system for the hash changes, looking at the links in /nix/var/nix/profiles to see them being added, looking really closely at your nixos-rebuild log output for errors/warnings, and from there backing up to your boot entries and seeing if something Linode-specific is failing to get updated/added there to reference new system profiles after they’re built.
If you’re seeing gotosocial added but no new caddy changes even across a reboot, that’s pretty weird behavior. Maybe try temporarily removing the entire services.caddy section to see if it goes away properly. Try cat /run/current-system/etc/caddy/caddy_config to see if it matches what you’re seeing in your filesystem’s /etc. If there’s a mismatch, something weird must be up with /etc, like mounting or permission issues.
It prints Unit gotosocial.service could not be found., so it does seem plausible that the config is just not being applied at all.
I’ll do some digging based on your post and report back. Really appreciate the help.
nixos-rebuild list-generations may also give some quick insight. Shows what generations it thinks you have and which is currently active. Might make it obvious if you’re either failing to create new ones or failing to change which is the current one.