Tutorial for setting up the LAMP stack on a NixOS server?

Thanks @jonringer! :slight_smile: This worked:

  services.httpd.virtualHosts."example.org" = {
    documentRoot = my-source.source-code;
    extraConfig = ''
      <Directory />
        DirectoryIndex index.php
        Require all granted
      </Directory>
      SetEnv MYSQL_HOST localhost
      SetEnv MYSQL_DATABASE ${dbConfig.db}
      SetEnv MYSQL_USER ${dbConfig.user}
      SetEnv MYSQL_PASSWORD ${dbConfig.password}
    '';
  };

Keep in mind this isn’t a good idea, though… as it exposes your password into the nix store which is world readable permission wise.

Given what you have previously posted you don’t actually need a password to authenticate to your database, you can use socket authentication instead.

If you want to have a working session on the LAMP with NixOS. It might be more efficient to handle various questions that could come up.

1 Like

Thanks again @aanderse ! I’ve successfully setup socket authentication. My configuration.nix now looks like this:

{ config, pkgs, ... }:
let
  my-source = import ./source-code-derivation.nix { inherit pkgs; };
  dbConfig = {
    db = "myDb";
    user = "myDbUser";
    socket = "'./run/mysqld/mysqld.sock'";
  };
in
{
  imports = [
    ./hardware-configuration.nix
  ];

  boot.cleanTmpDir = true;
  networking.hostName = "myhostname";
  networking.firewall.allowPing = true;
  services.openssh.enable = true;
  users.users.root.openssh.authorizedKeys.keys = [
    "ssh-ed25519 mykey myemail"
  ];

  environment.systemPackages = with pkgs; [
    neovim
  ];


  networking.firewall.allowedTCPPorts = [ 80 443 ];

  services.httpd.enable = true;
  services.httpd.adminAddr = "myemail";
  services.httpd.enablePHP = true; # oof... not a great idea in my opinion

  services.httpd.virtualHosts."example.org" = {
    documentRoot = my-source.source-code;
    extraConfig = ''
      <Directory />
        DirectoryIndex index.php
        Require all granted
      </Directory>
      SetEnv MYSQL_HOST localhost
      SetEnv MYSQL_DATABASE ${dbConfig.db}
      SetEnv MYSQL_USER ${dbConfig.user}
      SetEnv MYSQL_SOCKET ${dbConfig.socket}
    '';
  };

  services.mysql = {
    enable = true;
    package = pkgs.mariadb;
    bind = "localhost";
    ensureDatabases = [
      dbConfig.db
    ];
    ensureUsers = [
      {
        name = "${dbConfig.user}";
        ensurePermissions = {
          "${dbConfig.db}.*" = "ALL PRIVILEGES";
        };
      }
    ];
  };

}

Do you have any suggestions to decrease space usage in the nix store? I’d like this run this old app on a small VPS, and currently the nix store is taking about 4gb of space.

[root@extravmstianlagstad:/]# du /nix/ -sh
4.1G    /nix/

That’s after running nix-env -p /nix/var/nix/profiles/system --delete-generations +2 and nix-collect-garbage. Is it realistic/practical to decrease that 4gb?

I would suggest opening a new topic for your space question. I have never tried to reduce disk space usage with NixOS because I only work on boxes where high disk space usage is expected. Sorry I couldn’t be helpful on this topic.

Examine your system’s closure and see what is taking space.
nix path-info -rsSh /run/current-system.

For a NixOS container with your configuration (so without the “my-source” and hardware-configuration) i get a 1.4G closure size.