I am trying to expose an option from a module using mkOption
, I’m still new and have not been able to read through enough to now how to achieve this.
I’ve tried something like what is mentioned below
Continuing the discussion from Passing parameters into import:
#flake.nix
{
description = "Nixos config flake";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-24.05";
home-manager = {
url = "github:nix-community/home-manager/release-24.05";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, ... }@inputs:
let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
in
{
nixosConfigurations = {
oak = nixpkgs.lib.nixosSystem {
specialArgs = { inherit inputs; };
modules = [
./hosts/oak/configuration.nix
./modNixos
inputs.home-manager.nixosModules.default
];
};
samara = nixpkgs.lib.nixosSystem {
specialArgs = { inherit inputs; };
modules = [
./hosts/samara/configuration.nix
./modNixos
inputs.home-manager.nixosModules.default
];
};
};
};
}
Loading lamp.nix
in a group from default.nix
#default.nix
{ pkgs, lib, ... }:
{
imports = [
./services/lamp.nix
./system/nfs-mounts.nix
./services/php.nix
];
phpMod.enable = lib.mkDefault true;
}
lamp.nix
is where I set the mkOption, but I’m getting an error:
error: undefined variable 'webPath'
at /nix/store/qi0gjwwzgi3mmpc3vqss6i15y894bbz6-source/modNixos/services/lamp.nix:77:16:
76| extraConfig = ''
77| root * ${webPath}/${site}
| ^
78| php_fastcgi unix/${config.services.phpfpm.pools."${site}.${tld}".socket}
I would think that setting a default would do the trick, but I’m certainly missing something.
#lamp.nix
{ config, pkgs, lib, ... }:
let
php' = pkgs.php83.buildEnv {
extensions = ({ enabled, all }: enabled ++ (with all; [
xdebug
imagick
]));
extraConfig = ''
memory_limit = 1024M
xdebug.mode = debug
xdebug.start_with_request = yes
xdebug.idekey = gdbp
'';
};
options = {
lampWebRoot = lib.mkOption {
default = "/mnt/www";
example = "/the/web/path";
description = "Set Web Server Root Directory";
};
};
# config = lib.mkIf config.phpMod.enable {
config = {
webPath = config.lampWebRoot;
};
tld = "localhost";
# Add sites here and the rest will be handled... Caddy, Mariadb databases, directory creation and permissions.
sites = [
"devpress"
"ecotechie"
"nerdpress"
];
in
{
networking.hosts = {
"127.0.0.1" = map (site: "${site}") sites;
};
services.mysql.enable = true;
services.mysql.package = pkgs.mariadb;
services.mysql.ensureDatabases = map (site: "${site}") sites;
services.mysql.ensureUsers = [
# NOTE: it is important that `name` matches `$USER` name, this allows us to avoid password authentication
{ name = "sergio";
ensurePermissions = {
"*.*" = "ALL PRIVILEGES";
};
}
];
services.phpfpm.pools = lib.listToAttrs (map (site: lib.nameValuePair "${site}.${tld}" {
user = "sergio";
group = "users";
phpPackage = php';
settings = {
"listen.owner" = config.services.caddy.user;
"listen.group" = config.services.caddy.group;
"pm" = "dynamic";
"pm.max_children" = 5;
"pm.start_servers" = 2;
"pm.min_spare_servers" = 1;
"pm.max_spare_servers" = 5;
"php_admin_value[error_log]" = "stderr";
"php_admin_flag[log_errors]" = true;
"catch_workers_output" = true;
};
}) sites);
services.caddy.enable = true;
services.caddy.virtualHosts = lib.listToAttrs (map (site: lib.nameValuePair "https://${site}.${tld}:443" {
extraConfig = ''
root * ${webPath}/${site}
php_fastcgi unix/${config.services.phpfpm.pools."${site}.${tld}".socket}
file_server
''; }) sites);
# automatically create a directory for each site with appropriate ownership+permissions
systemd.tmpfiles.rules = map (site: "d ${webPath}/${site} 0755 sergio caddy") sites;
}
Also, how/where would I set webPath
or I guess it would be lampWebRoot
if I wanted it to be different for another host… Essentially, I want the default for all hosts, but different for the Samara
host declared in flake.nix