Okay, that's a great practice for keeping your Guix Home configuration manageable and modular! The core idea is to create a separate Guile module for your Sway configuration and then import and use it in your main home-configuration.scm.
Here's how you can achieve this:
1. Directory Structure (Suggestion)
It's good to organize your files. A common approach is:
~/.config/guix/
├── home-configuration.scm # Your main home configuration
├── modules/ # Directory for your custom modules
│ └── my-sway-config.scm # Your Sway specific configuration module
└── dotfiles/ # Directory for actual dotfiles managed by Guix
├── sway/
│ ├── config # Your Sway config file
│ └── status.sh # Example: your swaybar status script
├── waybar/
│ ├── config
│ └── style.css
└── mako/
└── config
You can adjust this, but the key is having a separate .scm file for the Sway service definition.
2. Create Your Sway Configuration Module (my-sway-config.scm)
This file will define a Guile module that exports your Sway service configuration.
;; ~/.config/guix/modules/my-sway-config.scm
(define-module (my-home-modules my-sway-config)
#:use-module (guix gexp)
#:use-module (gnu home services desktop) ; For home-sway-service-type
#:use-module (gnu packages wm) ; For sway, waybar, etc.
#:use-module (gnu packages suckless) ; For dmenu (if you use it)
#:use-module (gnu packages notifications) ; For mako
#:use-module (gnu packages xorg) ; For xwayland (if needed)
;; Add other package modules you need for Sway accessories
#:export (sway-service)) ; We will export a variable holding the service
;; Define the Sway service configuration
(define sway-service
(service home-sway-service-type
(home-sway-configuration
#:sway sway ; The sway package
#:config-file (local-file "../dotfiles/sway/config") ; Path relative to this .scm file
#:extra-packages (list
waybar
mako
dmenu ; or wofi, bemenu, etc.
;; Add other essential packages for your Sway setup:
;; e.g., grim, slurp, swaylock, swayidle, kanshi,
;; brightnessctl, pulseaudio-utils (or pipewire utils)
xwayland ; If you need X11 compatibility
)
;; If you manage Waybar through Guix Home as well
#:waybar-configuration
(home-waybar-configuration
#:waybar waybar ; The waybar package
#:config-file (local-file "../dotfiles/waybar/config")
#:style-file (local-file "../dotfiles/waybar/style.css"))
;; If you manage Mako through Guix Home
#:mako-configuration
(home-mako-configuration
#:mako mako
#:config-file (local-file "../dotfiles/mako/config"))
;; You can also embed simple configurations directly:
;; #:mako-configuration
;; (home-mako-configuration
;; #:mako mako
;; #:extra-options '("font=monospace 10"
;; "default-timeout=5000"))
;; Optional: Set environment variables specific to your Sway session
#:environment-variables
(list "WLR_NO_HARDWARE_CURSORS=1" ; Example
"XDG_CURRENT_DESKTOP=sway"
(string-append "PATH=" (getenv "HOME") "/.local/bin")) ; Prepend to existing PATH
;; Autostart applications (alternative to exec in sway config)
;; These are started via systemd user units or similar mechanisms
;; #:autostart (list (file-append kanshi "/bin/kanshi")) ; Example for kanshi
)))
;; You could also define a procedure if you want to parameterize it further
;; (define (make-sway-service #:key (theme 'dark))
;; (service home-sway-service-type
;; (home-sway-configuration ...)))
;; And then export 'make-sway-service'
Key points for my-sway-config.scm:
define-module: Names your module. (my-home-modules my-sway-config) is a good convention.
#:use-module: Imports necessary Guix modules.
(gnu home services desktop) provides home-sway-service-type, home-waybar-configuration, home-mako-configuration.
(guix gexp) for local-file.
- Package modules like
(gnu packages wm), (gnu packages notifications).
#:export (sway-service): Makes the sway-service variable (which holds your configured service) available for import in other files.
local-file: This is crucial. It takes a path relative to the .scm file it's in (my-sway-config.scm in this case) and makes that file available in the Guix store.
- Package Variables:
sway, waybar, mako are variables referring to the actual Guix packages. Make sure they are imported from their respective modules.
3. Modify Your Main home-configuration.scm
Now, import and use your new Sway module.
;; ~/.config/guix/home-configuration.scm
(use-modules (guix home)
(guix home services)
(gnu home services shells) ; For bash, zsh, etc.
;; ... other services you use ...
(my-home-modules my-sway-config)) ; <<< IMPORT YOUR SWAY MODULE
(home-configuration
#:home (home-environment ; Renamed from `home-directory` in newer Guix
#:home-directory "/home/your-username"
#:username "your-username")
#:packages (list
;; Your other global packages
)
#:services
(cons*
sway-service ; <<< USE THE EXPORTED SWAY SERVICE
;; (make-sway-service #:theme 'light) ; If you defined a procedure
(service home-bash-service-type
(home-bash-configuration
#:bashrc (list (local-file ".bashrc"))))
;; ... other services ...
%default-home-services)
;; Optional: Add your custom modules directory to Guile's load path
;; This is often not strictly necessary if `home-configuration.scm` and
;; the `modules` directory are in `~/.config/guix/`, as Guix Home
;; usually sets up the load path correctly for `guix home reconfigure`.
;; However, for `guile -L .` testing, it can be useful.
#:guile-load-path (list (string-append (getenv "HOME") "/.config/guix/modules"))
)
Key points for home-configuration.scm:
(use-modules ... (my-home-modules my-sway-config)): This imports the module you created. Guix needs to be able to find my-sway-config.scm. If it's in ~/.config/guix/modules/, and you run guix home reconfigure ~/.config/guix/home-configuration.scm, Guix Home usually adds ~/.config/guix/ to the load path, so modules/my-sway-config.scm will be found.
sway-service: This is the variable you exported from my-sway-config.scm. You add it to your list of services.
cons*: A common way to prepend your custom services to a base list like %default-home-services.
#:guile-load-path: This field in home-configuration can explicitly tell Guile where to find your custom modules. This is helpful if your modules are not in a standard location or for local testing with guile.
4. Update Your Sway Config (../dotfiles/sway/config)
Ensure your Sway config file (~/.config/guix/dotfiles/sway/config in the example structure) uses paths that will work when managed by Guix. For example, for scripts or other programs:
- Instead of
exec ~/.config/sway/status.sh, you might need to ensure status.sh is in your PATH (e.g., by putting it in ~/.local/bin/ and adding that to PATH) or use absolute paths to where Guix will place them if they are also managed as packages/gexps.
- Often, programs listed in
#:extra-packages (like waybar, mako) will be directly available in the PATH within the Sway session.
5. Reconfigure Your Home Environment
guix home reconfigure ~/.config/guix/home-configuration.scm
If you encounter "module not found" errors:
- Ensure the path in
(use-modules (my-home-modules my-sway-config)) correctly reflects the module name defined in my-sway-config.scm.
- Ensure
my-sway-config.scm is in a place Guix can find it. ~/.config/guix/modules/ is a good spot.
- You can try specifying the path to your modules directory explicitly in your
home-configuration.scm using #:guile-load-path as shown above.
- For testing,
cd ~/.config/guix and run guile -L . -c '(use-modules (my-home-modules my-sway-config))' to see if Guile can load your module.
Benefits of this approach:
- Modularity: Your main
home-configuration.scm stays cleaner.
- Reusability: You could potentially reuse
my-sway-config.scm (or parts of it) if you had multiple home configurations.
- Organization: Easier to find and manage specific parts of your configuration.
- Focus: When working on Sway, you only need to focus on
my-sway-config.scm and its related dotfiles.
This structured approach significantly improves the maintainability of your Guix Home setup, especially as it grows!