Of course. As a Guix expert, I can tell you that separating your configuration into logical, reusable modules is one of the most powerful features of using Guile as a configuration language. It's an excellent practice for maintainability, readability, and reusability.
Let's break down how to extract your Sway configuration from your main home.scm into its own dedicated file.
The Core Concept: Guile Modules
The fundamental mechanism we'll use is Guile's module system. We will:
- Create a new
.scm file to act as a Guile module. This file will contain only your Sway configuration.
- Define a variable inside this new module that holds the
sway-configuration record.
- Export that variable so other files can see it.
- Modify your main
home.scm to use the new module and reference the variable it exports.
- Tell Guix where to find your new module when you reconfigure.
Step-by-Step Guide
Let's assume your current Guix Home configuration lives in ~/.config/guix/home.scm.
Step 1: Create a Directory for Your Modules
It's good practice to keep your custom modules organized. Let's create a subdirectory.
mkdir -p ~/.config/guix/modules
Your directory structure will now look like this:
~/.config/guix/
├── home.scm
└── modules/
Step 2: Create the Sway Configuration Module
Now, create the file that will hold your Sway configuration. Let's call it ~/.config/guix/modules/my-sway.scm.
Inside this file, you'll define a module and a variable.
;; ~/.config/guix/modules/my-sway.scm
;; 1. Define the module. The name '(modules my-sway) is a convention
;; that matches the file path. Guile will look for 'modules/my-sway.scm'.
(define-module (modules my-sway)
#:use-module (gnu home services window-managers)
#:use-module (guix gexp))
;; 2. Define the variable that will hold our Sway configuration.
;; The name `sway-config` is arbitrary, but make it descriptive.
(define sway-config
(sway-configuration
(terminal "foot")
(gaps (sway-gaps-configuration (inner 8)))
(fonts (sway-fonts-configuration
(names '("Cantarell" "Noto Color Emoji"))
(style "Regular")
(size 11.0)))
;; 3. Use `gexp` for multi-line strings. This is the cleanest way.
(extra-config
#~(string-append
"
# Variables
set $mod Mod4
set $menu dmenu_run -l 10 -fn 'Cantarell-11'
# Keybindings
bindsym $mod+Return exec foot
bindsym $mod+d exec $menu
bindsym $mod+Shift+q kill
# Autostart applications
exec_always mako
exec_always waybar
"
;; You can even dynamically add more config here if needed
))))
;; 4. Export the variable so `home.scm` can import and use it.
;; This is crucial!
(define-public sway-config sway-config)
Explanation:
(define-module (modules my-sway) ...): Declares a new module named (modules my-sway). Guix will find this module by looking for a file named modules/my-sway.scm in its load path.
#:use-module ...: Just like in your home.scm, you need to import the definitions you use, like sway-configuration.
(define sway-config ...): This is a standard Scheme variable definition. It holds the entire sway-configuration record.
#~(string-append ...): We use a G-Expression (gexp) here for the extra-config. This is superior to string-append alone because it properly handles escaping and makes the configuration much cleaner to read and write.
(define-public sway-config sway-config): This makes the sway-config variable visible to other modules that import (modules my-sway). Without this line, the variable would be private to the module.
Step 3: Update Your Main home.scm
Now, let's edit your main ~/.config/guix/home.scm to use this new module.
;; ~/.config/guix/home.scm
(use-modules (gnu home)
(gnu home services)
(gnu home services window-managers)
;; 1. Import your newly created sway module.
(modules my-sway))
(home-environment
(packages
(list ;; your packages...
))
(services
(list
;; 2. Use the variable 'sway-config' that you exported from your module.
(service home-sway-service-type sway-config)
;; ... other services like shepherd, dbus, etc.
(service home-dbus-service-type)
)))
Notice how much cleaner home.scm becomes. The complex Sway configuration is now hidden away, and you just reference it with the simple sway-config variable name.
Step 4: Reconfigure with the Correct Load Path
This is the final, critical step. When you run guix home reconfigure, you must tell Guix where to find your custom modules directory. You do this with the -L flag.
guix home reconfigure -L ~/.config/guix ~/.config/guix/home.scm
-L ~/.config/guix: This tells Guix to add the ~/.config/guix directory to its module load path. When home.scm asks to (use-modules (modules my-sway)), Guix will now look inside ~/.config/guix/ and find the modules/my-sway.scm file.
You're done! Your Sway configuration is now cleanly separated.
Advanced Technique: Using a Procedure for Dynamic Configuration
What if you want your Sway configuration to depend on something from your home-environment, like your username? You can't do that with a static variable. The solution is to turn sway-config into a procedure (a function).
1. Modify modules/my-sway.scm:
Change the define to a define for a procedure that takes the home-environment record as an argument.
;; ~/.config/guix/modules/my-sway.scm (procedure version)
(define-module (modules my-sway)
#:use-module (gnu home) ; <-- Need this for home-environment-username
#:use-module (gnu home services window-managers)
#:use-module (guix gexp))
;; It's now a procedure that creates the configuration
(define (make-sway-config home-env)
(sway-configuration
(extra-config
#~(string-append
"output * bg "
;; Use a value from the home-environment record!
#$(home-environment-home-directory home-env)
"/Pictures/wallpaper.png fill"
"\n"
"exec_always waybar -c ~/.config/waybar/config-"
#$(home-environment-username home-env) ; <-- Dynamic username
"\n"))
;; ... other sway config
))
(define-public make-sway-config make-sway-config)
2. Modify home.scm:
Now you call the procedure, passing it the special %home-environment variable, which Guix provides for exactly this purpose.
;; ~/.config/guix/home.scm (procedure version)
(use-modules (gnu home)
(gnu home services)
(gnu home services window-managers)
(modules my-sway))
(home-environment
(services
(list
;; Call the procedure, passing the current home-environment
(service home-sway-service-type (make-sway-config %home-environment))
;; ... other services
)))
This procedural approach is incredibly powerful and is the "Guix way" to create highly reusable and context-aware configuration components. You can pass any parameters you want, not just %home-environment.