# # Cookbook Name:: kosmos-base # Recipe:: tmux # package "tmux" plugins_dir = node["kosmos-base"]["tmux"]["plugins_dir"] default_conf = node["kosmos-base"]["tmux"]["default_conf"] tpm_redux = node["kosmos-base"]["tmux"]["tpm_redux"] plugins = node["kosmos-base"]["tmux"]["plugins"] directory plugins_dir do owner "root" group "root" mode "0755" recursive true action :create end # Install tpm-redux (the plugin manager) at a pinned commit. git "#{plugins_dir}/tpm-redux" do repository tpm_redux["repo"] revision tpm_redux["revision"] user "root" group "root" action :sync end # Install each managed plugin at a pinned commit. Plugins are cloned by Chef # into a shared, root-owned directory so that users never need to install them # themselves. plugins.each do |shorthand, meta| git "#{plugins_dir}/#{shorthand.split('/').last}" do repository meta["repo"] revision meta["revision"] user "root" group "root" action :sync end end # System default tmux config: minimal — only the plugin manager environment, # the @plugin declarations for the chef-managed plugins, and the tpm-redux # initialisation line (which must be the last line of the file). template default_conf do source "tmux.conf.default.erb" owner "root" group "root" mode "0644" variables( plugins_dir: plugins_dir, plugins: plugins ) action :create end # Write a per-user ~/.tmux.conf. Users who submit their own overrides as a # cookbook file at files/default/tmux_users/.conf get a fully # managed config: it sources the system default (so chef-managed plugins # always load) and then appends the user's overrides. Users who have not # submitted a file get a blank importer written only if ~/.tmux.conf does # not already exist, so any hand-written/local config is left untouched. cookbook = run_context.cookbook_collection["kosmos-base"] search(:users, "groups:sysadmin AND NOT action:remove") do |u| username = u["username"] || u["id"] home_dir = u["home"] || "/home/#{username}" group_name = u["gid"] || username next unless Dir.exist?(home_dir) user_conf_source = "tmux_users/#{username}.conf" if cookbook.has_cookbook_file_for_node?(node, user_conf_source) disk_path = cookbook.preferred_filename_on_disk_location(node, :files, user_conf_source) # Chef reads ERB templates with IO.binread, so the template output buffer # is ASCII-8BIT. File.read returns UTF-8 on the node; concatenating a UTF-8 # string into an ASCII-8BIT buffer raises an encoding error on non-ASCII # content. Retag as ASCII-8BIT to match the buffer — the raw bytes are # still valid UTF-8, so the written file is correct. user_content = File.read(disk_path).force_encoding("ASCII-8BIT") template "#{home_dir}/.tmux.conf" do source "tmux.conf.user.erb" owner username group group_name mode "0644" variables(default_conf: default_conf, user_content: user_content) action :create end else template "#{home_dir}/.tmux.conf" do source "tmux.conf.user.erb" owner username group group_name mode "0644" variables(default_conf: default_conf, user_content: nil) action :create_if_missing end end end