mirror of
https://github.com/fscotto/infra.git
synced 2026-05-30 15:39:58 +00:00
The previous lineinfile-with-backrefs negative-lookahead regex was not appending NVIDIA parameters when missing. Switch to a clearer approach: slurp the file, extract the current GRUB_CMDLINE_LINUX value, build a merged target value that appends missing NVIDIA params, then write back the line. lineinfile sees no diff when the target matches the current line, preserving idempotency. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
74 lines
2.5 KiB
YAML
74 lines
2.5 KiB
YAML
---
|
|
- name: Read /etc/default/grub
|
|
tags: [packages, nvidia]
|
|
ansible.builtin.slurp:
|
|
src: /etc/default/grub
|
|
register: nymph_grub_default
|
|
|
|
- name: Extract current GRUB_CMDLINE_LINUX value
|
|
tags: [packages, nvidia]
|
|
ansible.builtin.set_fact:
|
|
nymph_grub_cmdline_current: >-
|
|
{{
|
|
(nymph_grub_default.content | b64decode).split('\n')
|
|
| select('match', '^GRUB_CMDLINE_LINUX="')
|
|
| first | default('GRUB_CMDLINE_LINUX=""')
|
|
| regex_replace('^GRUB_CMDLINE_LINUX="', '')
|
|
| regex_replace('"$', '')
|
|
}}
|
|
|
|
- name: Build merged GRUB cmdline with NVIDIA parameters
|
|
tags: [packages, nvidia]
|
|
ansible.builtin.set_fact:
|
|
nymph_grub_cmdline_target: >-
|
|
{{
|
|
(
|
|
nymph_grub_cmdline_current
|
|
~ (' nouveau.modeset=0' if 'nouveau.modeset=0' not in nymph_grub_cmdline_current else '')
|
|
~ (' nvidia-drm.modeset=1' if 'nvidia-drm.modeset=1' not in nymph_grub_cmdline_current else '')
|
|
) | trim
|
|
}}
|
|
|
|
- name: Ensure NVIDIA kernel parameters are present in GRUB cmdline
|
|
tags: [packages, nvidia]
|
|
ansible.builtin.lineinfile:
|
|
path: /etc/default/grub
|
|
regexp: '^GRUB_CMDLINE_LINUX='
|
|
line: 'GRUB_CMDLINE_LINUX="{{ nymph_grub_cmdline_target }}"'
|
|
state: present
|
|
register: nymph_grub_cmdline
|
|
|
|
- name: Regenerate GRUB configuration
|
|
tags: [packages, nvidia]
|
|
ansible.builtin.command: grub-mkconfig -o /boot/grub/grub.cfg
|
|
changed_when: true
|
|
when: nymph_grub_cmdline is changed
|
|
|
|
- name: Configure NVIDIA power management for hybrid graphics
|
|
tags: [packages, nvidia]
|
|
ansible.builtin.copy:
|
|
dest: /etc/modprobe.d/nvidia-power-management.conf
|
|
content: |
|
|
options nvidia "NVreg_DynamicPowerManagement=0x02"
|
|
owner: root
|
|
group: root
|
|
mode: "0644"
|
|
|
|
- name: Install prime-run wrapper script for NVIDIA PRIME offload
|
|
tags: [nvidia, dotfiles, dotfiles:host]
|
|
ansible.builtin.copy:
|
|
src: "{{ playbook_dir }}/../dotfiles/nymph/.local/bin/prime-run"
|
|
dest: "{{ user_home }}/.local/bin/prime-run"
|
|
owner: "{{ username }}"
|
|
group: "{{ user_group }}"
|
|
mode: "0755"
|
|
force: false
|
|
|
|
- name: Wrap alacritty with prime-run for NVIDIA PRIME offload
|
|
tags: [nvidia, dotfiles, dotfiles:desktop, dotfiles:host]
|
|
ansible.builtin.lineinfile:
|
|
path: "{{ user_home }}/.config/i3/config"
|
|
regexp: '^bindsym \$mod\+Return exec --no-startup-id /usr/bin/alacritty'
|
|
line: 'bindsym $mod+Return exec --no-startup-id ~/.local/bin/prime-run /usr/bin/alacritty'
|
|
when: "'i3' in (desktop_sessions_enabled | default([]))"
|