Setting up a WordPress dev environment. AKA... Trying to run before I can walk

wonderful to hear

i am glad that NixOS has become productive for you!

1 Like

I have this flake.nix file inside my Wordpress development theme directory

{
  description = "WordPress development shell";

  inputs = {
    nixpkgs.url = "nixpkgs/nixos-24.05";
    nixos-shell.url = "github:Mic92/nixos-shell";
  };

  outputs = { self, nixpkgs, nixos-shell, ... }@inputs:
  let
    pkgs = nixpkgs.legacyPackages.x86_64-linux;
    start =
      pkgs.writeShellScriptBin "start" ''
        set -e
        export QEMU_NET_OPTS="hostfwd=tcp::8080-:80"
        ${pkgs.nixos-shell}/bin/nixos-shell --flake .
       '';
  in {
    nixosConfigurations.vm = nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      specialArgs.inputs = inputs;
      modules = [
        ({ lib, config, pkgs, ... }: {

          nixpkgs.overlays = [
            (self: super: {
              wordpress = super.wordpress.overrideAttrs (oldAttrs: rec {
                postInstall = let
                  wpConfig = pkgs.writeTextFile {
                    name = "wp-config-localhost.php";
                    text = ''
                      <?php
                        $table_prefix  = 'wp_';
                        require_once('/var/lib/wordpress/localhost/secret-keys.php');
                        define('DB_CHARSET', 'utf8');
                        define('DB_HOST', 'localhost:3306');
                        define('DB_NAME', 'wordpress');
                        define('DB_PASSWORD', "");
                        define('DB_USER', 'wordpress');
                        define('FS_METHOD', 'direct');
                        if ( !defined('ABSPATH') )
                          define('ABSPATH', dirname(__FILE__) . '/');
                        require_once(ABSPATH . 'wp-settings.php');
                      ?>
                    '';
                  };
                in ''
                  ln -s ${wpConfig} $out/share/wordpress/wp-config.php
                  rm -r $out/share/wordpress/wp-content
                  ln -s /var/lib/wordpress/localhost/wp-content $out/share/wordpress/wp-content
                '';
              });
            })
          ];

          services.wordpress = {
            webserver = "nginx";
            sites."localhost" = { };
          };

          services.nginx.virtualHosts."localhost".root = lib.mkForce "${pkgs.wordpress}/share/wordpress";

          systemd.tmpfiles.rules = [
            "d /var/lib/wordpress/localhost 0750 wordpress caddy - -"
            "d /var/lib/wordpress/localhost/wp-content 0750 wordpress caddy - -"
            "d /var/lib/wordpress/localhost/wp-content/plugins 0750 wordpress caddy - -"
            "d /var/lib/wordpress/localhost/wp-content/themes 0750 wordpress caddy - -"
            "d /var/lib/wordpress/localhost/wp-content/upgrade 0750 wordpress caddy - -"
            "d /var/lib/wordpress/localhost/wp-content/uploads 0750 wordpress caddy - -"
          ];

          nixos-shell.mounts.extraMounts = {
            "/var/lib/wordpress/localhost/wp-content/themes/icynets-simplic" = {
               target = /home/onny/projects/icynets-simplic;
               cache = "none";
            };
          };

          system.stateVersion = "24.05";
          services.getty.autologinUser = "root";
          documentation = {
            info.enable = false;
            man.enable = false;
            nixos.enable = false;
          };
          nix = {
            package = pkgs.nixFlakes;
            registry.nixpkgs.flake = inputs.nixpkgs;
            settings.experimental-features = [ "nix-command" "flakes" ];
          };
        })
        nixos-shell.nixosModules.nixos-shell
      ];
    };

    packages = { inherit start; };
    defaultPackage.x86_64-linux = start;

  };
}

with nix run I’m able to access the Wordpress instance at http://localhost:8080 with the theme mounted to the writeable wp-content directory.

Would like to make this more elegant without writing a custom wp-config.php file but not sure how to do it yet

1 Like

It’s been a loooong time, but I’m at the point where I’m feeling the base nature of the setup you help me with.

Would you be willing to share how you would/do go about setting up a WordPress dev environment? You mentioned devenv, and I’m thinking that may be the way forward.

Maybe the approach that @MatthieuB shared using devenv up and process compose

i still think devenv is going to be your best bet - Domen has a company around these types of services so he’s motivated to continue providing support and great documentation… from everything i’ve seen he’s doing a great job

i recommend you give it a go, and maybe you would consider posting about your results back here so people in the future can learn from your adventures


if that isn’t something you’re interested in and you want something specific, feel free to ask

1 Like

I’ve managed to get a decent setup going for Drupal dev environments, using Services-flake. I’m liking this a lot, and it should work pretty well for WordPress as well. GitHub - freelock/drupal-flake: A flake to jump-start Drupal development – this could easily be forked into a version for WordPress.

2 Likes