module Pacman

Wrapper around system Arch Linux pacman

Public Instance Methods

init(path, sysroot, config, mirrors, arch:'x86_64', env:nil) click to toggle source

Configure pacman for the given root @param path [String] is the path on the host for pacman @param sysroot [String] path to the system root to use @param config [String] config file path to use, note gets copied in @param mirrors [Array] of mirror paths to use, mirror file name is expected to be the

name of the repo e.g. archlinux.mirrorlist

@param arch [String] capturing the pacman target architecture e.g. x86_64 @param env [Hash] of environment variables to set for session

# File lib/nub/pacman.rb, line 42
def init(path, sysroot, config, mirrors, arch:'x86_64', env:nil)

  # All configs are on the sysroot except config and cache
  mirrors = [mirrors] if mirrors.is_a?(String)
  self.path = path
  self.arch = arch
  self.env = env
  self.sysroot = sysroot
  self.log = File.join(sysroot, 'var/log/pacman.log')
  self.db_path = File.join(self.sysroot, 'var/lib/pacman')
  self.gpg_path = File.join(self.sysroot, 'etc/pacman.d/gnupg')
  self.hooks_path = File.join(self.sysroot, 'etc/pacman.d/hooks')
  self.repos = mirrors.map{|x| File.basename(x, '.mirrorlist')}
  mirrors_path = File.join(self.sysroot, 'etc/pacman.d')
  self.mirrors = mirrors.map{|x| File.join(mirrors_path, File.basename(x))}

  # Config and cache are kept separate from the sysroot
  self.config = File.join(self.path, File.basename(config))
  self.cache_path = File.join(self.path, 'cache')

  # Validate incoming params
  Log.die("pacman config '#{config}' doesn't exist") unless File.exist?(config)

  # Copy in pacman files for use in target
  FileUtils.mkdir_p(self.db_path)
  FileUtils.mkdir_p(self.gpg_path)
  FileUtils.mkdir_p(self.hooks_path)
  FileUtils.mkdir_p(self.cache_path)
  FileUtils.cp(config, self.config, preserve: true)
  FileUtils.cp(mirrors, mirrors_path, preserve: true)

  # Update the given pacman config file to use the given path
  FileUtils.replace(self.config, /(Architecture = ).*/, "\\1#{self.arch}")
  FileUtils.replace(self.config, /#(DBPath\s+= ).*/, "\\1#{self.db_path}")
  FileUtils.replace(self.config, /#(CacheDir\s+= ).*/, "\\1#{self.cache_path}")
  FileUtils.replace(self.config, /#(LogFile\s+= ).*/, "\\1#{self.log}")
  FileUtils.replace(self.config, /#(GPGDir\s+= ).*/, "\\1#{self.gpg_path}")
  FileUtils.replace(self.config, /#(HookDir\s+= ).*/, "\\1#{self.hooks_path}")
  FileUtils.replace(self.config, /.*(\/.*mirrorlist).*/, "Include = #{mirrors_path}\\1")

  # Initialize pacman keyring
  if !File.exist?(File.join(self.gpg_path, 'trustdb.gpg'))
    Sys.exec("pacman-key --config #{self.config} --init")
    Sys.exec("pacman-key --config #{self.config} --populate #{repos * ' '}")
  end
end
install(pkgs, flags:nil) click to toggle source

Install the given packages @param pkgs [Array] of packages to install @param flags [Array] of params to pass into pacman

# File lib/nub/pacman.rb, line 99
def install(pkgs, flags:nil)
  if pkgs && pkgs.any?
    cmd = []

    if self.sysroot
      cmd += ["pacstrap", "-c", "-M", self.sysroot, '--config', self.config]
    else
      cmd += ["pacman", "-S"]
    end

    # Add user flags
    cmd += flags if flags

    # Add packages to install
    cmd += ['--needed', *pkgs]

    # Execute
    self.env ? Sys.exec(cmd, env:self.env) : Sys.exec(cmd)
  end
end
remove_conflict(pkgs) click to toggle source

Remove the given conflicting packages @param pkgs [Array] of packages to remove

# File lib/nub/pacman.rb, line 122
def remove_conflict(pkgs)
  if pkgs && pkgs.any?
    cmd = "pacman -Rn"
    cmd += " -r #{self.sysroot}" if self.sysroot
    cmd += " -d -d --noconfirm #{pkgs * ' '} &>/dev/null || true"
    Sys.exec(cmd)
  end
end
update() click to toggle source

Update the pacman database

# File lib/nub/pacman.rb, line 90
def update
  cmd = "pacman -Sy"
  cmd += " --config #{self.config}" if self.config
  Sys.exec(cmd)
end